Unit 1 and 2 Java Fundamentals
Unit 1 and 2 Java Fundamentals
program
Pascal case: ThisIsName - is used with classes
Save as : Demo.java
Compile: javac Demo.java
Run: java Demo
Command line arguments
When user wants to pass information into a
program
Whatever is passed to main() is stored as
String array
class CommandLineExample{
public static void main(String args[]){
System.out.println("Your first argument is: "+
args[0]);
}
}
Java Basic
Lexical Issues- White space, Identifiers,
Literals, comments, Separators
Variable Declaration
type identifier = value;
Java Keywords
Integer
Floating point
Character type
Java uses unicode to represent character.
16 bit. Range is 0 to 65536
Type Conversion and Casting
Explicit conversion
(target-type) value
Arrays
type var-name[ ]; or type[ ] var-name;
Scope and lifetime of variable
Operators – Arithmetic, Bitwise, Relational,
Logical, Assignment.
? Operator
exp1 ? exp2 : exp3
Arithmetic
Relational
Control statements
Selection Statements- if-else, switch case
its index.
Index starts for 0 to n-1.
One dimensional, two dimensional, 3
dimensional etc.
Size of array is found in its length instance
variable.
1D arrays
Type var-name[];
Var-name = new type[size];
E.g. Int num[];
num = new int[5]; or
int num= new int[5];
Arrays can be initialized when they are
declared.
E.g. int num[] = {1,2,3,4,5};
Multidimensional array
Int twoD[][] = new int[4][5];
or
Int twoD[][]= new int[2][];
twoD[0]= new int[5];
twoD[1]= new int[5];
Or initialize array when declaring
Int twoD[][]={{1,2},{3,4}};
Char array is declared as String object
String str=“This is a test”;
Multi Dimensional Array
Program
Java Scanner
Java provides various ways to read input from
the keyboard, the java.util.Scanner class is
one of them.
we can get input from the user in primitive