Java
Fundamentals
First simple Java program
public class TestJava{ public static void main(String args[]){ System.out.println(“Welcome To My World”); } } Step 01 Compiling Java Program >javac YourJavaFileName.java Step 02 Running Java Program >java YourJavaFileName
Data Types
1. Integers – This group includes byte, short, int and long, which are for wholevalued signed numbers. 2. Floating-point numbers – This group includes float and double, which represent number with fractional precision 3. Characters – This group includes char, which represent symbols in a character set, like letters and numbers. 4. String - This group includes whole words. 5. Boolean – This group includes Boolean, which is a special type for representing true/false values.
Integers
This includes four type of sub category a. long – long is a signed 64-bit type and is useful for those occasions where an int type is not larger enough to hold desired value. b. int – The most commonly used integer type is int. ( 32-bits) c. short – short is a signed 16-bit type. It is probably the least-used Java type. d. byte – The smallest integer type is byte. Byte may not be directly compatible with Java’s other built-in types.
Floating-point numbers
Floating-point numbers also known as real numbers are used when evaluating expressions that require fractional precision. Two kind of floating point types float and double. a. float The type float specifies a single-precision value that uses 32 bits of storage. Faster on some processors and takes half as much space as double precision, but will become imprecise when the values are either very large or very small. Java Fundamentals Kasun Jayasena ( B. Sc. (Hons) Applied Science – University of Peradeniya ) 1
b. double Double precision uses 64 bits to store a value. Double precision is actually faster than single precision on some modern processors that have been optimized for high speed mathematical calculations.
Characters
In java, the data type used to store characters is char. The range of a char is 0 to 65,536. There are no negative chars.
Booleans
Java has a simple type, called Boolean, for logical values. It can have only one of tow possible values, true or false. Definition of each variable type 1. Integer long long days; int int days; short short days; byte byte days; 2. Floating-point numbers float float days; double double days; 3. Characters char ch; 4. Boolean boolean b; 5. String String name; Creating and Initializing Variables Ex: Integer – int 1. Creating variable int num1; int num2; or int num1, num2; 2. Initializing variable num1=0; num2=0; Java Fundamentals Kasun Jayasena ( B. Sc. (Hons) Applied Science – University of Peradeniya ) 2
or num1=0, num2=0; String – String 1. Creating variables String name; 2. Initializing variables name=”Kasun Jayasena"; Q01: Write a simple program to add two integers called “x” and “y” in to a single integer variable called “z”. Display format will be shown below. 1. “ x add y equal z” ( Ex: 10 add 25 equal 35) 2. “ x + y =z” (Ex: 10+25=35) Q02: Defined two String variables and initialized its according your first name and last name. Add this two String variables and display it as shown below. 1. My Full Name: Kasun Jayasena
Java Control Statements
1. The if statement if(condition) { statement(s); } Else{ statement(s); } 2. Nested if statements If(condition1){ statement(s); } else if(condition2){ statement(s); } . . . else{ statement(s); }
Java Fundamentals Kasun Jayasena ( B. Sc. (Hons) Applied Science – University of Peradeniya )
3
3. switch switch(expression){ case Value1: statements break; case Value2: statements break; default: default statements } 4. while while(condition){ body of loop; } Ex: while(n>0){ System.out.println(n); n--; //n=n-1; }//end loop 5. do-while loop do{ body of loop; }while(n>0);
6. for loop for (initialization; condition; iteration){ statement(s); } Q03: Print your name 10 times using for loop.
7. Nested for loop for (initialization; condition; iteration){ for(initialization; condition; iteration){ statement(s); }//close second for loop }//close first for loop
Java Fundamentals Kasun Jayasena ( B. Sc. (Hons) Applied Science – University of Peradeniya )
4
Q04: Design following pattern using two for loops ********** ********* ******** ******* ****** ***** **** *** ** * 8. Using break to Exit a loop for(int i=0;i<100;i++){ if(i==20) break; System.out.println(i); } System.out.println(“Loop Terminate”);
Arrays
1D Array
The general form of one-dimensional array declaration is type var-name[]; Ex: int days[]; In here allocate memory for an array using new keyword. days=new int[7]; or int days[]=new int[7];
Q05: Create 1D array for display the 7 days of a week. Note: use for loop to display.
2D Array
The general form of two-dimensional array declaration is type var-name[][]; Ex: int twoD[][]; In here allocate memory for an array using new keyword. twoD=mew int[4][5];
Introducing Classes
The class is the core of Java. It is the logical construct upon which the entire Java language is built because it defined the shape and nature of an object. As such, the class forms the basis for object-oriented programming in Java.
Java Fundamentals Kasun Jayasena ( B. Sc. (Hons) Applied Science – University of Peradeniya )
5
Then General Form of a Class
The general form of a class definition is shown here: class Classname{ type instance-variable1; type instance-variable2; . . . type instance-variableN; type methodname1(parameter-list){ body of method1 }//end methodname1 type methodname2(parameter-list){ body of method2 }//end methodname2 type methodnameN(parameter-list){ body of methodN }//end methodnameN
}//ens of Class
A Simple Class
class Box{ double width; double heigth; double depth; }//end of first class class BoxDemo{ public static void main(String args[]){ //Creating object of the first class Box Box myBox=new Box(); //assign values to myBox’s instance variables myBox.width=10; myBox.heigth=20; myBox.depth=10; //get the totoal value to variable called “vol” double vol; vol= myBox.width* myBox.heigth* myBox.depth; System.out.println(“Volume of the Box:”+vol); } }//end of main class Java Fundamentals Kasun Jayasena ( B. Sc. (Hons) Applied Science – University of Peradeniya ) 6