Lecture 1 Introduction To Java Programming
Lecture 1 Introduction To Java Programming
Writing in the Java programming language is the primary way to produce code
that will be deployed as Java byte code, though there are compilers available for
other languages such as JavaScript, Python and Ruby, and a native Java scripting
language called Groovy. Java syntax borrows heavily from C and C++ but it
eliminates certain low-level constructs such as pointers and has a very simple
memory model where every object is allocated on the heap and all variables of object
types are references. Memory management is handled through integrated automatic
garbage collection performed by the Java Virtual Machine (JVM).
Page 1
Al-Farabi University College
class Example1 {
public static void main(String args[])
{ int var1; // this declares a variable
int var2; // this declares another variable
var1 = 1024; // this assigns 1024 to var1
System.out.println("var1 contains " + var1);
var2 = var1 / 2;
System.out.print("var2 contains var1 / 2: ");
System.out.println(var2); }
}
Mathematical Operators
class Example2 {
public static void main(String args[])
{ int iresult, irem;
double dresult, drem;
iresult = 10 / 3;
irem = 10 % 3;
dresult = 10.0 / 3.0;
drem = 10.0 % 3.0; S
ystem.out.println("Result and remainder of 10 / 3: " + iresult +
" " + irem);
Page 2
Al-Farabi University College
Logical Operators
class Example3 {
public static void main(String args[]) {
int n, d; n = 10; d = 2;
if(d != 0 && (n % d) == 0)
System.out.println(d + " is a factor of " + n);
d = 0; // now, set d to zero
// Since d is zero, the second operand is not evaluated.
if(d != 0 && (n % d) == 0)
System.out.println(d + " is a factor of " + n);
/* Now, try same thing without short-circuit operator. This will
cause a divide-by-zero error. */
if(d != 0 & (n % d) == 0)
System.out.println(d + " is a factor of " + n); } }
Output// 2 is a factor of 10
Blocks of Code
class Example4 { public static void main(String args[]) { double
i, j, d; i = 5;
j = 10;
if(i != 0) {
System.out.println("i does not equal zero");
d = j / i;
System.out.print("j / i is " + d);
} System.out.println(); } }
Output// i does not equal to zero
Page 3
Al-Farabi University College
j/i is 2
Page 4
Al-Farabi University College
int b = Integer.parseInt(temp);
int c = Integer.parseInt(temp);
JOptionPane.showMessageDialog(null, "Average is " + (a+b+c)/3); }
}
One has to note that the input is stored as a string, temp, and then parsed to integer
using the method parseInt( ). This time the method accepts a parameter, temp, and
returns an integer. When the above program is executed, a dialog box will appear on
screen with a field to accept input from user via keyboard
(JOptionPane.showInputDialog). This is repeated three times and finally the result
is again displayed in a dialog box (JOptionPane.showMessageDialog).
Page 5
Al-Farabi University College
if-else-if Ladder
class Ladder { public static void main(String args[]) {
int x;
{ if(x==1)
System.out.println("x is one");
else if(x==2)
System.out.println("x is two");
else if(x==3)
System.out.println("x is three");
else if(x==4)
System.out.println("x is four");
Else
Output//
x is one
x is two
x is three
x is four
{ int i;
Page 6
Al-Farabi University College
switch(i)
break;
break;
break;
break;
case 4:
System.out.println("i is four");
break;
default:
{ result = 1; e = i;
while(e > 0)
{ result *= 2; e--; }
Output//
2 to the power of 0 is 1
2 to the power of 1 is 2
2 to the power of 2 is 4
2 to the power of 3 is 8
Page 7
Al-Farabi University College
{ int i;
{ if((i%2) != 0) continue;
System.out.println(i); } } }
Output//
100
Page 8