Session-2 - BuzzWords ByteCode Programs
Session-2 - BuzzWords ByteCode Programs
Session -2 Assignments
1. Programming Assignment
a. Find the Greatest of 3 numbers
b. Arrange 3 numbers in ascending / descending
order.
c. Grade of a student using switch.
[Write in class work backside Give Title – Conditional Statements]
1. Basic Structure of Java Program
//Approach -1
public class Simple
{
public static void main(String args[])
{
System.out.println("Welcome To Smart Learning of Java");
}
}
At compile time, java file is compiled by Java Compiler (It does not
interact with OS) and converts the java code into byte code.
Bytecode Verifier: checks the code fragments for illegal code that can violate access
right to objects.
Translating a Java program into byte code makes it much easier to run a
program in a wide variety of environments because only the JVM needs to
be implemented for each platform.
Once the run-time package exists for a given system, any Java program
can run on it. Remember, although the details of the JVM will differ from
platform to platform, all understand the same Java byte code.
The execution of byte code by the JVM is the easiest way to create truly
portable programs.
The fact that a Java program is executed by the JVM also helps to make it
secure. Because the JVM is in control, it can contain the program and
prevent it from generating side effects outside of the system.
1. Simple
2. Object-Oriented
3. Portable
4. Platform independent
5. Secured
6. Robust
7. Architecture neutral
8. Dynamic
9. Interpreted
10. High Performance & Multithreaded
11. Distributed
Simple
According to Sun, Java language is simple because:
Object-oriented
Object-oriented means we organize our software as a combination
of different types of objects that incorporates both data and
behavior.
1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
Platform Independent
The Java platform differs from most other platforms in the sense that it is a
software-based platform that runs on the top of other hardware-based
platforms. It has two components:
1. Runtime Environment
2. API(Application Programming Interface)
Java code can be run on multiple platforms e.g. Windows, Linux, Sun Solaris,
Mac/OS etc. Java code is compiled by the compiler and converted into
bytecode. This bytecode is a platform-independent code because it can be run
on multiple platforms i.e. Write Once and Run Anywhere(WORA).
Secured
o No explicit pointer
o Java Programs run inside virtual machine sandbox
These security are provided by java language. Some security can also
be provided by application developer through SSL, JAAS,
Cryptography etc.
Robust
Architecture-neutral
Portable
High-performance
Java is faster than traditional interpretation since byte code is
"close" to native code still somewhat slower than a compiled
language (e.g., C++)
Distributed
We can create distributed applications in java. RMI and EJB are
used for creating distributed applications. We may access files by
calling the methods from any machine on the internet.
Multi-threaded
if(condition)
{
//code to be executed
}
o if-else statement (two way)
if(condition)
{
//code to be executed
}
else
{
//code to be executed
}
o nested if statement ( if inside another if)
if(condition1)
{
if(condition2)
{
//code to be executed
}
//code to be executed
}
o if-else-if ladder (Multi way selection)
if(condition1)
{
//code to be executed
}
else if(condition2)
{
//code to be executed
}
-----------------------
else
{
//code to be executed
}
switch(expression)
{
case value1: //code to be executed;
break; //optional
case value2: //code to be executed;
break; //optional
......
default:
// code to be executed if all cases are not matched;
}
{
public static void main(String[] args)
{
int number=20;
switch(number)
{
case 10: System.out.println("10");
case 20: System.out.println("20");
case 30: System.out.println("30");
default:System.out.println("Not in 10, 20 or 30");
}
}
}
//Program to check a number if Even or Odd
if( a % 2 == 0)
System.out.println(" Number is Even " + a);
else
System.out.println(" Number is Odd " + a);
}
}
//Program to Find grade of a Student in 3 subjects
switch(choice)
{
case "ADD" : c = a+b;break;
case "SUB" : c = a-b;break;
case "MUL" : c = a*b;break;
case "DIV" : c = a/b;break;
case "REM" : c = a%b;break;
default : System.out.println("Invalid Operation");
}
}
}