Java Programming - Module 1
Java Programming - Module 1
Agenda
Evolution of Java Java Architecture Language Basics Arrays
Objectives
At the end of this module, you will be able to :
Write the first Java Program with understanding of Language Basics and Keywords Understand Array declaration, definition and access of
array elements
Evolution of Java
Key Founders
Java was the brainchild of: James Gosling Patrick Naughton Chris Warth Ed Frank & Frank Sheridan
The origin of Java can be traced back to the fall of 1992, and was
initially called Oak Oak was renamed as Java in 1995
Design Goal
Java was originally meant to be a platform-neutral language for embedded software in devices The goal was to move away from platform and OS-specific compilers that would compile source for a particular target platform to a language that would be portable, and platform-independent The language could be used to produce platform-neutral code
Java Architecture
Java Architecture
Compile-time Environment step1
Java Source (.java)
step3
Run-time Environment
Java Class Libraries
Class Loader
Bytecode Verifier
step4 step5
Java Virtual machine
step2
Java Compiler
Java Interpreter
Step5:
JVM reads bytecodes and translates into machine code for execution. While execution of the program the code will interact to the operating system and hardware
3. Loading Use a class loader to read bytecodes from .class file into memory
Class Loader
Execution Engine
Interpreting a Java program into byte code facilitates its execution in a wide variety of environments
Once the Java runtime package exists for a given system, any Java program can run on it
The JVM will differ from platform to platform, and is, platform-specific All versions of JVM interpret the same Java byte code
The use of bytecode enables the Java runtime system to execute programs much faster
Java facilitates on-the-fly compilation of bytecode into native code
Quiz
1. A. B. C. D. E. F. Write the correct order of the Java program execution Class Loader Interpretation Compilation Byte Code Verification Java Source Code Execution
2. A Sample.Java file contains, class A, B and C. How many .class files will be created after compiling Sample.java?
Langugage Basics
public class Welcome { public static void main(String args[]) { System.out.println(Welcome to Java Programming); } }
Create source file: Welcome.java Compile : javac Welcome.java Execute : java Welcome
The Java API, included with the JDK, describes the function of each of its components
In Java programming, many of these components are precreated and commonly used
Language Basics
Keywords Data Types Variables Operators Conditional Statements Loops
Java Keywords
abstract assert boolean break byte case catch continue default do double else enum extends for goto if implements import instanceof int new package private protected public return short switch synchronized this throw throws transient try
char
class const
final
finally float
interface
long native
static
strictfp super
void
volatile while
Minimum Range
Maximum Range
+127 +32767
-32768
int
long float double char boolean
32
64 32 64 16 1
-2147483648
-9223372036854775808 1.40E-45 4.94065645841246544e-324d
+2147483647
+9223372036854775807 3.40282346638528860e+38 1.79769313486231570e+308d 0 to 65,535
0
0L 0.0f 0.0d '\u0000' false
NA
NA
Types of Variables
The Java programming language defines the following kinds of Variables: Local Variables Tied to a method Scope of a local variable is within the method Instance Variables (Non-static) Tied to an object Scope of an instance variable is the whole class Static Variables Tied to a class Shared by all instances of a class
Quiz
1. Match the followig table:
DATA TYPES SIZE(bytes)
char byte
int double
4 2
1 8
2. What will be the output for the below code ? public class Sample{ public static void main(String a[]){ int i_val; System.out.println("i_val value: "+i_val); }
}
Operators
Java provides a set of operators operations. Types of operators in java are, Arithmetic Operators Unary Operator Relational Operators Logical Operators Simple Assignment Operator Bitwise Operators to manipulate
Arithmetic Operators
The following table lists the arithmetic operators Operator Description Example
+ * / %
Addition Subtraction
Multiplication Division Modulus
A+B A-B
A*B A/B A%B
class Sample{ public static void main(String[] args){ int a = 10; int b = 3; System.out.println("a + b = " + (a + b) System.out.println("a - b = " + (a - b) System.out.println("a * b = " + (a * b) System.out.println("a / b = " + (a / b) System.out.println("a % b = " + (a % b) } }
); ); ); ); );
Unary Operators
The following table lists the unary operators
Operator + ++ -Description Unary plus operator Unary minus operator Increment operator Decrement operator Example +A -A ++A or A++ --A or A--
Relational Operators
The following table lists the relational operators
Operator == != Description Two values are checked, and if equal, then the condition becomes true Two values are checked to determine whether they are equal or not, and if not equal, then the condition becomes true Two values are checked and if the value on the left is greater than the value on the right, then the condition becomes true. Two values are checked and if the value on the left is less than the value on the right, then the condition becomes true Two values are checked and if the value on the left is greater than equal to the value on the right, then the condition becomes true Two values are checked and if the value on the left is less than equal to the value on the right, then the condition becomes true Example (A == B) (A != B)
>
(A > B)
<
(A < B)
>=
(A >= B)
<=
(A <= B)
class Sample{ a == b = false public static void main(String[] args){ a != b = true int a = 10; a > b = false a < b = true int b = 20; System.out.println("a == b = " + (a == b) ); b >= a = true b <= a = false System.out.println("a != b = " + (a != b) ); System.out.println("a > b = " + (a > b) ); System.out.println("a < b = " + (a < b) ); System.out.println("b >= a = " + (b >= a) ); System.out.println("b <= a = " + (b <= a) ); } }
Logical Operators
The following table lists the logical operators
Operator && Description This is known as Logical AND & it combines two variables or expressions and if and only if both the operands are true, then it will return true Example (A && B) is false
||
This is known as Logical OR & it combines two variables or expressions and if either one is true or both the operands are true, then it will return true
(A || B) is true
class Sample{ public static void main(String[] args){ boolean a = true; boolean b = false; System.out.println("a && b = " + (a&&b) System.out.println("a || b = " + (a||b) System.out.println("!(a && b) = " + !(a } }
); ); && b) );
Quiz
What will be the output for the below code ?
public class Sample{ public static void main(){ int i_val = 10, j_val = 20; boolean chk; chk = i_val < j_val; System.out.println("chk value: "+chk); }
}
Arrays
Arrays
An array is a container object that holds a fixed number of values of a single type When an array is created, the length of an array is fixed Array elements are automatically initialized with the default value of their type, When an array is created Array can be created using the new keyword Ex: int[] x = for 5 blocks new int[5]; // defining an integer array
Arrays (Contd.).
Alternatively, we can create and initialize array as below format int[] x = {10, 20, 30}; int[] x = new int[]{10, 20, 30};
Here the length of an array is determined by the number of values provided between { and } The built-in length property determines the size of any array Ex: int[] x = new int[10]; int x_len = x.length;
Array - Example
public class ArrayDemo { public static void main(String[] args) { int[] x; // declares an array of integers x = new int[5]; // allocates memory for 5integers x[0] = 11; X[4] = 22; System.out.println("Element at index 0: " + x[0]); System.out.println("Element at index 1: " + x[1]); System.out.println("Element at index 4: " + x[4]); } }
new array
int x[] = new int [5]; x= new int [10];
Array copy
To copy array elements from one array to another array, we can use arraycopy static method from System class Syntax: public static void arraycopy(Object s,int sIndex,Object d,int dIndex,int lngth) Ex:
int source[] = {1, 2, 3, 4, 5, 6}; int dest[] = new int[10];
System.arraycopy(source,0, dest,0,source.length);
Two-Dimensional Arrays
Two-dimensional arrays are arrays of arrays Initializing two-dimensional arrays:
int[][] y = new int[3][3];
The 1st dimension represent rows or number of one dimension, the 2nd
arrays Ex:
int[][] y = { {1,2,3}, {4,5,6}, {7,8,9} }; int[][] y = new int[3][] { {1,2,3}, {4,5,6}, {7,8,9} };
The length of the columns can vary for each row and initialize number of columns in each row Ex1:
int [][]x = new int [2][]; x[0] = new int[5]; x[1] = new int [3];
0 1
0 0
0 0
0 0 0
Ex2:
int [][]x = new int [3][]; x[0] = new int[]{0,1,2,3}; x[1] = new int []{0,1,2}; x[2] = new int[]{0,1,2,3,4};
j++) {
Output:
000 11 22222
Quiz
Select which of the following are valid array definition 1. int[] a; a = new int[5]; 2. int a[] = new int[5] 3. int a[5] = new int[5]; 4. int a[] = {1,2,3}; 5. int[] a = new int[]{1,2,3}; 6. int[] a = new int[5]{1,2,3,4};
Quiz (Contd.).
Predict the output
class Sample{ public static void main(String[] args){ int[] a = new int[5]{1,2,3}; for(int i : a) System.out.println(i); } }
Summary
In this session, you were able to :
Learn about Evolution of Java and forces that shaped it Understand Java Architecture along with JVM Concepts Write the first Java Program with understanding of Language Basics and Keywords Understand Array declaration, definition and access of array elements
References
1. Oracle (2012). Java Tutorials: Primitive Data Types. Retrieved on May 12, 2012, from, http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
2. Oracle (2012). Java Tutorials: Assignment, Arithmetic, and Unary Operators. Retrieved on May 12, 2012, from, http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
3. Schildt, H. Java: The Complete Reference. J2SETM. Ed 5. New Delhi: McGraw Hill-Osborne, 2005. 4. Tutorialpoint TP (2012). C-Operator Types. Retrieved on May 12, 2012, from, http://www.tutorialspoint.com/ansi_c/c_operator_types.htm
Thank You