Day 1
Day 1
Day 1
Portable
Java is portable because it facilitates you to carry the Java bytecode
to any platform. It doesn't require any implementation.
Multi-threaded
We can write Java programs that deal with many tasks at once by
defining multiple threads.
FEATURES OF JAVA
Dynamic
Java is a dynamic language. It supports the dynamic loading of classes.
It means classes are loaded on demand.
Interpreted
• The program are compiled into Java Virtual Machine (JVM) code
called bytecode
• Each bytecode instruction is translated into machine code at the
time of execution
FEATURES OF JAVA
Distributed
• Java is distributed because it facilitates users to create distributed
applications in Java.
• Fully supports IPv4, with structures to support IPv6
• Includes support for Applets: small programs embedded in HTML
documents
• RMI and EJB are used for creating distributed applications.
• This feature of Java makes us able to access files by calling the
methods from any machine on the internet.
POPULARITY OF JAVA
JAVA PLATFORMS
13
Quiz
1. Write the correct order of the Java program execution
A. Class Loader
B. Interpretation
C. Compilation
D. Byte Code Verification
E. Java Source Code
F. Execution
Quiz
class FirstProgram
{
public static void main(String args[])
{
System.out.println(“This is my First Java Program”);
}
}
Understand First Program
• A Java source file can contain multiple classes, but only one class
can be a public class.
• The source file name must match the name of the public class
defined in the file with the .java extension.
• A public class is accessible across packages.
• Body of every member function of a class (called method in Java)
must be written when the method is declared.
Understand First Program
public static void main(String[] args)
• main is the starting point of every Java application
• public is used to make the method accessible by all
• static is used to make main a static method of class.
• Static methods can be called by JVM without using any object; just using
the class name.
• void means main does not return anything
• String args[ ] represents an array of String objects that holds the
command line arguments passed to the application.
Understand First Program
System.out.println()
– Used to print a line of text followed by a new line
– System is a class inside the Java API
– out is a public static member of class System
– out represents the standard output (similar to stdout or cout)
– println is a public method of the class of which out is an object
We can use the plus operator (+) to concatenate multiple String
objects and create a new String object.
How to Execute a Java Program
1. Using Java Online Compiler
2. Using JDK and Notepad
3. Using Editors and JRE
PATH
Method Names
➢ Methods should be verbs, in mixed case with the first letter
lowercase, with the first letter of each internal word capitalized
➢ Eg: void run(), void getColor()
Try it and Tell me
Sample.java
class A {
void m1() { }
}
class B {
void m2() { }
}
class C {
void m3() { }
}
Try it and Tell me
class A {
void m1() { }
}
public class B {
void m2() { }
}
class C {
void m3() { }
}
You can access these arguments in your program, using the String
array that you have passed as an argument to the main method.
String[] args
Command line arguments
class Argument {
public static void main(String[] args) {
System.out.println(args[0]);
}
}
Example :
Example:
If the two command line arguments are Wipro and Bangalore then the
output generated should be Wipro Technologies, Bangalore, India.
If the command line arguments are ABC and Mumbai then the output
generated should be ABC Technologies, Mumbai, India
where args is the String array that we pass to the main method and
length is the property of the Array Object
Try it and Tell me
class FindLength{
public static void main(String[ ] args) {
int len = args.length;
System.out.println(len);
}
}
4.94065645841246544e-
double 64 1.79769313486231570e+308d 0.0d
324d
char 16 0 to 65,535 '\u0000'
boolean 1 NA NA false
Try it and Tell me
What will be the result, if we try to compile and execute the
following code?
class Test {
public static void main(String args[]) {
byte b=128;
System.out.println(b);
}
}
Try it and Tell me
What will be the result, if we try to compile and execute the
following code?
class Test {
public static void main(String ar[]) {
double f=1.2;
boolean b=1;
System.out.println(f);
System.out.println(b);
}
}
Try it and Tell me
class Test {
public static void main(String ar[]) {
float f=1;
float b=1.2;
float c=1.2323f;
System.out.println(f);
System.out.println(b);
System.out.println(c);
}
}
Try it and Tell me
class FloatExample
{
public static void main(String args[])
{
float d=987654321.1234567f;
float c=6.123456789f;
System.out.println(d);
System.out.println(c);
}
}
Try it and Tell me
class Test {
int a=10,b=017,c=0X3A;
System.out.println(a+","+b+","+c);
}
Basically if an assigned integer value begins with 0,it is considered to be an octal value.
If the assigned integer value begins with 0x it is taken as hexadecimal value.
Type Casting
Type casting is when you assign a value of one primitive data type to
another type.
Widening Casting (automatically) - converting a smaller type to a larger
type size
byte -> short -> char -> int -> long -> float -> double
double -> float -> long -> int -> char -> short -> byte
Try it and Tell me
class WideType {
public static void main(String[] args) {
int myInt = 9;
double myDouble = myInt;
System.out.println(myInt);
System.out.println(myDouble);
}
}
Try it and Tell me
class NarrowCast{
public static void main(String[] args) {
double myDouble = 9.78d;
int myInt = (int) myDouble;
System.out.println(myDouble);
System.out.println(myInt);
}
}
Parsing
Parsing in its most general sense is the extraction of the necessary
information from some piece of data, most often textual data.
There are many Java classes that have the parse() method.
Usually the parse() method receives some string as input, "extracts" the
necessary information from it and converts it into an object of the calling
class.
Integer
class Test {
public static void main(String [ ] ar) {
int for=2;
System.out.println(for);
}
}
Operators
• Java provides a set of operators to manipulate operations.
• Types of operators in java are,
– Arithmetic Operators
– Unary Operator
– Relational Operators
– Logical Operators
– Simple Assignment Operator
– Bitwise Operators
Arithmetic Operators
Operator Description Example
+ Addition A +B
- Subtraction A -B
* Multiplication A *B
/ Division A/B
% Modulus A%B
Do It Yourself
Write a program to accept two arguments of integer type and perform all
the arithmetic operations and display the outputs.
Example: java Calc 20 5
Output:
The value of A is 20 and B is 5
The result of A + B is 25
The result of A - B is 15
The result of A * B is 100
The result of A / B is 4
The result of A%B is 0
Unary Operators
!= Two values are checked to determine whether they are equal or not, and if not (A != B)
equal, then the condition becomes true
> Two values are checked and if the value on the left is greater than the value (A > B)
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 (A < B)
right, then the condition becomes true
>= Two values are checked and if the value on the left is greater than equal to the (A >= B)
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 (A <= B)
on the right, then the condition becomes true
Try it and Tell me
class Sample{
public static void main(String[] args){
int a = 10;
int b = 20;
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("b >= a = " + (b >= a) );
System.out.println("b <= a = " + (b <= a) );
}
Logical Operators
Operator Description Example
&& This is known as Logical AND & it combines two variables or (A && B) is false
expressions and if and only if both the operands are true, then
it will return true
! Called Logical NOT Operator. It reverses the value of a !(A && B) is true
Boolean expression
Try it and Tell me
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) );
}
}
Shift Operators <<and >>
• The shift operators(<< and >>) shift the bits of a number to the
left or right, resulting in a new number.
• They are used only on integral numbers( and not on floating point
numbers, i.e. decimals).
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
Left Shift Operator <<
• When we apply the left shift operator <<, the value gets multiplied
by 2 to the power of number specified after the operator.
int x = 8;
x = x << 4;
• 8 will be multiplied by the value 2 to the power of 4, which is 16.
• The result is 128.
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
Do It Yourself
Write a program to accept two numbers.
Perform the following operations
1. Left shift the First number by second number
of times
2. Right Shift the first number by second number
of times
Bitwise Operators
The bitwise operators take two bit numbers, use OR/AND to determine
the result on a bit by bit basis.
}
}
Bitwise ^ Operators
class BitwiseExample3 {
public static void main(String[] args) {
int x = 5;
int y = 9; 5-> 0101
int z = x ^ y; 9-> 1001
System.out.println("z = "+z);
1100
}
}
Do It Yourself
Write a program to accept two numbers.
Perform the following operations
1. Bitwise AND operation on a and b
2. Bitwise OR operation on a and b
3. Bitwise EXOR operation on a and b
Do It Yourself
Write a program to print all the arguments passed to a FindArguments.java
program.
Input: Number of arguments can be 0 to 12
Output Format:
The Number of Arguments are :
Example: java FindArguments Hi Nandan
The Following are the Arguments: Output:
The Number of Arguments are : 3
The Following are the Arguments:
1 Hi
2 Sai
3 Kiran
THANK YOU