Lecture 2
Lecture 2
Java Source
Code
Java Compiler
Java Enabled
Browser Java Interpreter
Output
Output
SIMPLE JAVA PROGRAM-EXAMPLE 1
/*This is a simple java program*/
class Example
{
public static void main (String args[])
{
System.out.println (“This is a simple Java program”);
}
}
SIMPLE JAVA PROGRAM-SOME
IMPORTANT POINTS
public: Access specifier. main() must be made public,
since it must be called by code defined outside it’s class.
Static: It is required because main() is called without
creating an object of it’s class
String args[]: An array of objects of type String class.
Each object of type string contains a character string. It
is used to manipulate command line argument.
Java is case sensitive.
System predefined class that refers to system.
out It is static data member of System class
println() It is a member of out object
JRE AND JDK
JRE: Java Runtime Environment
provides
libraries,
float 32 bits
double 64 bits
CHAR
It uses unicode to represent character.
The char type is unsigned 16 bit values ranging from 0 to 65536.
ASCII still ranges from 0 to 127.
Example:
class test
{
public static void main (String args[])
{
char ch1, ch2;
ch1=88;
ch2=‘Y’;
System.out.println (“ch1 and ch2: “ + ch1+” “+ch2);
}
}
Output: ch1 and ch2: X Y
CHAR
Example:
class test
{
public static void main (String args[])
{
char ch1;
ch1= ‘X’;
Sytem.out.println (“ch contains “+ch1);
ch1++;
System.out.println (“ch1 is now “ + ch1);
}
}
Output:
ch1 contains X
Ch1 is now Y
BOOLEANS
Size is 1 bit – two value: true and false.
This type is returned by all relational operators.
Example:
boolean b;
b= true;
1. System.out.println(“b is “+b);
2. System.out.println(“10>9 is “ +(10>9));
Output:
b is true
10>9 is true
LITERALS
Integer Literals
1. base 10 – 1,2,43 etc.
2. Any whole number is by default integer (32 bits).
• To specify a long literal, the number should appended with
an upper- or lowercase L.
LITERALS
Floating point Literals
1. Standard Notation – 3.14159, 0.6667, 2.0 etc.
2. Scientific Notation – 6.022E23, 2e+100.
• Floating point literals are by default of type double.
• To specify a float literal, we must append an F or f to the
constant.
LITERALS
Boolean Literals
• Two values – true and false.
• True is not equal 1 and false is not equal to 0.
• They can be assigned to variable declared as boolean.
LITERALS
Character Literals:
Can be converted into integers and manipulated with the integer operators.
• A literal character is represented inside a pair of single quotes.
1. \’ Single quote
2. \” Double quote
3. \\ Backslash
4. \r Carriage Return
5. \n New line
6. \t Tab
7. \b Backspace
LITERALS
String Literals
• A sequence of characters between a pair of double
quotes.
• In java string must begin and end on the same line.