Unit 1-Java Basics-I.pptx
Unit 1-Java Basics-I.pptx
class Demo
{
public static void main(String args[])
{
System.out.println(“WELCOME TO JAVA”);
}
}
• Compile: javac ClassName.java
– Javac Demo.java
• EXECUTE: java ClassName
– Java Demo
Java Keywords
• Difference between Post Increment (n++) and
Pre Increment (++n)
– Post Increment (n++): It increases the value of
variable by 1 after execution of the statement.
– Pre Increment (++n): It increases the value of
variable by 1 before execution of the statement.
Prime Number Logic: Prime Number are divisible by itself only.
sum n sum
sum+n
0 1 1
1 2 3
3 3 6
6 4 10
10 5 15
15 6 21
21 7 28
28 8 36
36 9 45
45 10 55
Factorial
• Logic: Factorial of 5 = 5 x 4 x 3 x 2 x 1
prod n prod
prod*n
1 5 5
5 4 20
20 3 60
60 2 120
120 1 120
Common Mistakes
• Some common errors in the initial phase of learning
programming:
- Parentheses Mismatch Eg: (()
- If there is a missing semicolon(;)
- The way method/class is used- case sensitivity
2. Write a program that prints the following line on the screen along with
quotes.
class Light {
public static void main(String args[]) {
OUTPUT:
int lightspeed; In 1000 days light will travel about
long days; 16070400000000 miles.
long seconds;
long distance;
// approximate speed of light in miles per second
lightspeed = 186000;
days = 1000; // specify number of days here
seconds = days * 24 * 60 * 60; // convert to seconds
distance = lightspeed * seconds; // compute distance
System.out.print("In " + days);
System.out.print(" days light will travel about ");
System.out.println(distance + " miles.");
}
}
• Floating-Point Types
TO Compute Area of Circle
class Area {
public static void main(String args[]) {
double pi, r, a;
r = 10.8; // radius of circle
pi = 3.1416; // pi, approximately
a = pi * r * r; // compute area
System.out.println("Area of circle is " + a);
}
}
Characters
• Java char is 16 bits type
– Range from 0-65536
– No negative chars
– The standard set of characters known as ASCII still
ranges from 0 to 127.
– The extended 8-bit character set, ISO-Latin-1,
ranges from 0 to 255
Demonstrate char data type.
class CharDemo {
public static void main(String args[]) {
char ch1, ch2;
ch1 = 88; // code for X
ch2 = 'Y';
System.out.print("ch1 and ch2: ");
System.out.println(ch1 + " " + ch2);
}
}
OUTPUT:
ch1 and ch2: X Y
char variables behave like integers
class CharDemo2 {
public static void main(String args[]) {
char ch1;
ch1 = 'X';
System.out.println("ch1 contains " + ch1);
ch1++; // increment ch1
System.out.println("ch1 is now " + ch1);
}
OUTPUT:
} ch1 contains X
ch1 is now Y
Booleans
• It can have only two values
– true/false
OUTPUT:
// Demonstrate boolean values.
b is false
class BoolTest {
b is true
public static void main(String args[]) {
This is executed.
boolean b;
10 > 9 is true
b = false;
System.out.println("b is " + b);
b = true;
System.out.println("b is " + b);
if(b) System.out.println("This is executed.");
b = false;
if(b){
System.out.println("This is not executed.");
}
// outcome of a relational operator is a boolean value
System.out.println("10 > 9 is " + (10 > 9));
}
}
class DynInit {
public static void main(String args[]) {
double a = 3.0, b = 4.0;
// c is dynamically initialized
double c = Math.sqrt(a * a + b * b);
System.out.println("Hypotenuse is " + c);
}
}
class Scope {
public static void main(String args[]) {
int x; // known to all code within main
x = 10;
if(x == 10) {
int y = 20; // known only to this block
// x and y both known here.
System.out.println("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here
// x is still known here.
System.out.println("x is " + x);
}
}
class LifeTime {
public static void main(String args[]) {
int x;
for(x = 0; x < 3; x++) {
int y = -1;
System.out.println("y is: " + y);
y = 100;
System.out.println("y is now: " + y);
}
OUTPUT:
} y is: -1
} y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100
Predict the output
class ScopeErr {
public static void main(String args[]) {
int bar = 1;
{
int bar = 2;
}
}
}
byte a = 40;
byte b = 50;
byte c = 100;
int d = a * b / c;
WRONG WAY
CORRECT
byte b = 50;
byte b = 50;
b = b * 2; //ERROR
b = (byte)(b * 2);
The Type Promotion Rules
• type promotion rules that apply to expressions
– First, all byte, short, and char values are promoted to
int
– if one operand is a long, the whole expression is
promoted to long
– If one operand is a float, the entire expression is
promoted to float.
– If any of the operands is double, the result is double.
class Promote {
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i / c) - (d * s);
System.out.println((f * b) + " + " + (i / c) + " - " + (d * s));
System.out.println("result = " + result);
}
238.14 + 515 - 126.3616
} result = 626.7784146484375
Arrays
• An array is a group of like-typed variables that are referred to by a common
name
• One-Dimensional Arrays
– Array declaration:
• type var-name[ ];
– Eg:
• int month_days[];
– array-var = new type[size];
– Eg:
• month_days = new int[12];
• month_days[1] = 28;
• System.out.println(month_days[3]);
class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");
}
}
April has 30 days
• An array initializer is a list of comma-separated
expressions surrounded by curly braces
class AutoArray {
public static void main(String args[]) {
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
System.out.println("April has " + month_days[3] + " days.");
}
}
class Average {
public static void main(String args[]) {
double nums[] = {10.1, 11.2, 12.3, 13.4, 14.5};
double result = 0;
int i;
for(i=0; i<5; i++)
result = result + nums[i];
System.out.println("Average is " + result / 5);
}
}
Average is 12.299999999999999
Multidimensional Arrays
• In Java, multidimensional arrays are actually arrays
of arrays
– int twoD[][] = new int[4][5];
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++){
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++; OUTPUT:
} 01234
} 56789
for(i=0; i<4; i++) { 10 11 12 13 14
for(j=0; j<5; j++) 15 16 17 18 19
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
int twoD[][] = new int[4][];
twoD[0] = new int[5];
twoD[1] = new int[5];
twoD[2] = new int[5];
twoD[3] = new int[5];
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++) {
OUTPUT:
for(j=0; j<i+1; j++) {
0
twoD[i][j] = k;
12
k++;
345
}
6789
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
double m[][] = {
{ 0*0, 1*0, 2*0, 3*0 },
{ 0*1, 1*1, 2*1, 3*1 },
{ 0*2, 1*2, 2*2, 3*2 },
{ 0*3, 1*3, 2*3, 3*3 }
};
System.out.println(num);
}
} 5368770908
1073741816
} 2147483632
-32
The Right Shift
• The right shift operator, >>, shifts all of the bits
in a value to the right a specified number of
times.
– general form
• value >> num
• int a = 32;
• a = a >> 2; // a now contains 8
• int a = 35;
•
class HexByte {
Public static void main(String args[]) {
char hex[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
byte b = (byte) 0xf1;
System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
}
}
OUTPUT:
b = 0xf1
The Unsigned Right Shift
• if you are shifting something that does not represent a numeric
value,
– you may not want sign extension to take place
• If you want to shift a zero into the high-order bit no matter what
its initial value was.
– This is known as an unsigned shift(>>>).
• int a = -1;
• a = a >>> 24;
11111111 11111111 11111111 11111111 –1 in binary as an int
>>>24
00000000 00000000 00000000 11111111 255 in binary as an int
// Unsigned shifting a byte value.
class ByteUShift {
static public void main(String args[]) {
char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7‘, '8', '9', 'a', 'b', 'c', 'd', 'e', 'f’ };
byte b = (byte) 0xf1;
byte c = (byte) (b >> 4);
byte d = (byte) (b >>> 4);
byte e = (byte) ((b & 0xff) >> 4);