Java Application

1. Java applications
Let us start our introduction to Java with a basic application. It looks like this:
Public class We1come1
{
public static void main (string args [ ] )
{
system.out.printin ( "Fast-Trak to Java!") ;
}
// end of main ( )
}
// end of class we1come1

This makes no sense, right? We will provide a basic overview and then get started with actually chucking out java code. The first line of the program is the class definition for we1come1. It is a good practice to begin the class name with an upper –case letter. This program is saved in a file called we1come1, java. The rest of the code is within this class. The compiler compiles the same file and generates a we1come1, class which is eventually run on the Java Virtual Machine (JVM). we1come1 here is an identifier, which is a user defined word, consisting of letters, digits, - (underscore). $ (a dollar sign).
Note: An identifier cannot begin with a digit.

1.1 Building an application
We will start by building an application that adds two numbers. Whenever you build any Java Application, there is a set template for design that you automatically follow.
impot javax.swing.JOPtionPane;
The computer imports classes you will be using in your Application.
impot javax.swing.JOPtionPane;
Public class addition
{
} // end of class Addition

Difine your class name, and right away place the opening and closing bracket—with the comment.
impot javax.swing.JOPtionPane;
Public class addition
{
Public static void main (string args [ ] )
{
system.exit(0);
} // end of main ( )
} // end of class Addition

Add the main method, and system. exit (0)
Next, we actually start with the program logic for the intended application.
import javax.swing.JoptionPane;
public class Addition
{
public static vold main (string args [ ] )
{
starting firstNumber, secondNumber;

These two are "string" references. That means they have the potential to point to objects of type string. However, at this point they are empty references.
import javax.swing.JoptionPane;
public class Addition
{
public static vold main (string args [ ] )
{
starting firstNumber, secondNumber;
Int number, number2, sum;
system.exit (0) ;
} // end of main ( )
} // end of class Addition

Now, we add three integer variables. They are not objects, and hold three integers (with any methods or classes). number1, number2 and number3 are called primitive variables.
import javax.swing.JoptionPane;
public class Addition
{
public static vold main (string args [ ] )
{
starting firstNumber, secondNumber;
Int number, number2, sum;
firstNumber = JoptionPane.sgowInputDialog ("First Num") ;
secondNumber = Joptionpane.sgowInputDialog ("second Num") :

Method showInputDialg ( ) receives a string argument, and returns a string result. Joptionpane creates InputDialog boxes.
But, since the numbers are still strings, we can't add them yet. We need some object that has a method capable of taking a string argument and returning an integer.
import javax.swing.JoptionPane;
public class Addition
{
public static vold main (string args [ ] )
{
starting firstNumber, secondNumber;
Int number, number2, sum;
firstNumber = JoptionPane.sgowInputDialog ("First Num") ;
secondNumber = Joptionpane.sgowInputDialog ("second Num") :
number1 = Integer. parseInt (firstNumber) ;
number2 = Integer. parseInt (secondNumber) ;
sum = number1+number2;

Integer is a class. Its method parseInt ( ) takes a string argument and returns an int.
Add finally, the code looks like:
public class Addition
{
public static vold main (string args [ ] )
{
starting firstNumber,
secondNumber;
Int number1,
number2,
sum;
firstNumber = JoptionPane.sgowInputDialog ("First Num") ;
secondNumber = Joptionpane.sgowInputDialog ("second Num") :
number1 = Integer. parseInt (firstNumber) ;
number2 = Integer. parseInt (secondNumber) ;
sum = number1+number2;
JOPtionPane.showMessageDialog (null, "The sum is : " + sum,
"Results," Joptionpane.PLAIN_ MESSAGE) ;
system.exit (0) ;
} // end of main ( )
} // end of class Addition

The method show MessageDialog of class JOPtionPane takes four argument:
i) null - - this will be explained in a later chapter
ii) "The sum is:" + sum - - this converts the int sum into a string and concatenates it with the string "The sum is:"
iii) "Results" is the message displayed in the title bar.
iv) JOPtionPane.PLAIN_MESSAGE difines the icon.
For the icons, you have five alternate contants to choose from:
1) JOPtionPane.PLAIN_MESSAGE
2) JOPtionPane.ERROR_MESSAGE
3) JOPtionPane.INFORMATION_MESSAGE
4) JOPtionPane.WARNING_MESSAGE
5) JOPtionPane.QUESTION_MESSAGE

IN Java, constants are always all upper case, with words separated by underscores.
Note: we concatenated a string with an int: "The sum is"+sum.
The sequence is as follows: first the sum is converted from an integer to a string. Which is then concatenated with the string "The sum is: ". A common problem lot of people make is writing the code in the following way
Int number1 = 2;
int number2 = 4;
JOPtionPane.showMessageDialog (null,
"The sum is: "+ number1+number2;
"Screwy Result", JOPtionPane.WARNING_MESSAGE) ;
1.2 Date type
A variable called number1 actually refers to a place in memory where the value of the variable is stored. Every variable in Java has the following attributes: name, type, size, and a value.
1.2.1 Name
Variable names must begin with a letter. After than they can contain digits, the $ symbol and underscores. Java uses Unicode for its characters, so any "letter" that is valid for a word in any world language is valid for a name in Java.
1.2.2 Type
The "type" appears before the identifier name. It can be one of the "primitive data types" or it can be any previously defined class.
int num1;
This is a declaration. At this point, the name num1 refers to a location (a pointer) in the computer's RAM where this variable is stored. When an int is declared, four bytes are set aside. Still, nothing is stored in it yet.
1.2.3 Size
When we assign a type [int, string] to a variable, we are not only declaring memory location. We also decide how big of a number or character is able to be stored in that variable.
1.2.4 Value
Finally,value is what we want the variable to store.
1.3 Primitive data types
Java is a Strongly-typed language. That means, every variable must be declared as a type. In Java, there are 8 primitive types.
• 6 of those refer to numbers
-- 4 for integer's types,
-- 2 for floating- point types,
• 1 is the character type char, used for characters in Unicode encoding, and
• 1 is a Boolean type for true or false values.
1.3.1 Int
In contrast to C/C++, an int always-no matter which operating system-take 4 bytes of storage space. Because those 4 bytes are set in stone, you can be sure that every JVM that runs your program will be able to store the same size numbers.
int is the most commonly used size number.
Range:- 2,147,483,648 to 2147,483,647 (over two billion)
1.3.2 Short
In Java, a short is defined as 2 bytes, no matter which operating system is used. You would only use this for special situations, such as when speed is really crucial. For VB programmers, a short is what you' ve come to think of as an int.
Range :- 32,768 to 32,767
1.3.3 Long
A long is defined as 8 bytes, no matter which operating system is used.
Range: - 9,223,372,036,854,775,808L to 9,223,372,036,854,775,807L
Please notice the upper-case L suffix is appended to any long. This is required.
Hexadecimal numbers have a prefix:ox
Ox1CFE.
1.3.4 byte
A byte is defined as 1 byte, no matter which operating system is used
Range :- 128 to 127
Again, like a short, a byte is only used under rare circumstances.
1.3.5 float
A float is defined as 4 bytes, no matter which operating system is used.
Range: approximately +/- 1.79769313486231570 E+308 (15 significant decimal digits). This is the most common choice for any decimal, double is the default, not flat; therefore, no special character is appended.
1.3.7 char
A char is defined as 2 bytes, irrespective of the operating system used. A char type always refers to a character in the Unicode encoding scheme. Approximately 65,536 characters can be represented. Single quotes denote a char constant. 'H' is a char constant. "H" is string that contains a single character – it is not a char. This is syntax error, and the compiler will give you an error.
1.3.8 boolean
A Boolean type has only two values. In contrast to C/C++, in Java, 0 and 1 cannot stand in for true or false. A Boolean type must be assigned the value of the constants true or false.
1.4 Java math operators
• Addition +
• Subtraction -
• Multiplication *
• Division /
• Modulus %
All are binary operators, i,e, they work with two numbers. They are executed according to the rules for operator precedence. (There is no operator for exponentiation in Java). Let us see some of the commonly overlooked things regarding Math operators in Java.
1.4.1 Multiplication*
Consider the following code:
int x = 2:
double y = 3.889, sum = 0.000;
sum = y * x:
The integer will be temporarily converted to a double and two doubles will be multiplied. Afterward, the original integer remains unchanged.
Rules for temporary conversions:
First priority: If either of the operands is of type double, then the other operand is converted to double for the calculation.
Second priority: If either of the operands is of type float, then the other operand is converted to float for the calculation.
Third priority: If any of the operands is of type long, then the other operand is converted to long for the calculation.
note: these conversions are automatic, because none of them result in loss of accuracy.
Static casts
When you convert a double to float, then information is inevitably lost, you can accomplish this using a cast;
int x 2, sum = 0:
double y= 3.889;
sum = (int) y * x'
{sum is now equal to 6}
Here, a value of just 3 will be used for y If you want to round y, you a method from class Math:
sum = (int) Math.round(y)* x:
1.4.2 Division /
Division can lead to unexpected results. For example, if both operands are integers, then the result of the division is also an integer. Any fractional part of the division is discarded. As a result, 17/3=5.
1.4.3 %
In contrast to the division operator, modulus returns the remainder of any division. The modulus operator can only be used when both operands are integers. Thus, 17%3=2.
1.5 Comparison operators
These are used for selection structures:
• equality = =
• not equal !=
• greater than >
• less than < • greater than equal >=
• less than or equal <=
The equality operator is a common source of mistakes; Note that two equal signs are always used. The single equal sigh [=] is only used for assignment, that is, assigning the value on the right to the variable on the left assignment, that assigning the value on the right to the variable on the left.
num = 33;
When you make a compound symbol using the equal sign, the equal sing should always be placed on the right.