0% found this document useful (0 votes)
63 views

Session-2 - BuzzWords ByteCode Programs

Session 2 of the Java course will cover: 1) Recapping Session 1 concepts 2) Explaining the basic structure of a Java program 3) Discussing Java bytecodes 4) Describing common Java terminology ("buzz words") 5) Learning about conditional statements in Java programs Students will also be assigned programming tasks involving conditional statements.

Uploaded by

andrajub4u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views

Session-2 - BuzzWords ByteCode Programs

Session 2 of the Java course will cover: 1) Recapping Session 1 concepts 2) Explaining the basic structure of a Java program 3) Discussing Java bytecodes 4) Describing common Java terminology ("buzz words") 5) Learning about conditional statements in Java programs Students will also be assigned programming tasks involving conditional statements.

Uploaded by

andrajub4u
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Session -2 Agenda

1. Recap of Session-1 [10 Minutes]


2. Basic Structure of Java Program [20 Minutes]
3. Java Byte code [20 Minutes]
4. Java Buzz Words [30 Minutes]
5. Programs - Conditional Statements [30 Minutes]

o Program to check a number is even or odd.


o Program to find grade of a student in 3 subjects
o Program to find arithmetic operations using
switch.

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");
}
}

File Name : Simple.java


Compile : javac Simple.java
Execute/Run : java Simple
//Approach -2
class Simple
{
public static void main(String args[])
{
System.out.println("Welcome To Smart Learning of Java");
}
}

File Name : demo.java


Compile : javac demo.java
Execute/Run : java Simple
o class keyword is used to declare a class in java.
o public keyword is an access modifier which represents
visibility, it means it is visible to all.
o static is a keyword, if we declare any method as static, it
is known as static method. The core advantage of static
method is that there is no need to create object to invoke
the static method. The main method is executed by the
JVM, so it doesn't require creating object to invoke the
main method. So it saves memory.
o void is the return type of the method, it means it doesn't
return any value.
o main represents startup of the program.
o String[] args is used for command line argument.
o System.out.println() is used print statement.
2. Java Byte Code (Java Magic)
What happens at compile time?

At compile time, java file is compiled by Java Compiler (It does not
interact with OS) and converts the java code into byte code.

What happens at runtime?

At runtime, following steps are performed:

Classloader: is the subsystem of JVM that is used to load class files.

Bytecode Verifier: checks the code fragments for illegal code that can violate access
right to objects.

Interpreter: read byte code stream then execute the instructions.


Byte Code:
 The output of a Java compiler is not executable code. Rather, it is byte
code.

 Byte code is a highly optimized set of instructions designed to be executed


by the Java run-time system, which is called the Java Virtual Machine
(JVM).
 In essence, the original JVM was designed as an interpreter for byte code.

 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.

 In general, when a program is compiled to an intermediate form and then


interpreted by a virtual machine, it runs slower than it would run if
compiled to executable code.

 To improve performance JIT Compiler is used (Just In Time Compiler). JIT


compiles parts of the byte code that have similar functionality at the same
time, and hence reduces the amount of time needed for compilation and
during execution.
3. Features of Java / Java Buzz Words
There is given many features of java. They are also known as java buzzwords.
The Java Features given below are simple and easy to understand.

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:

Syntax is based on C++ (so easier for programmers to learn it


after C++).

removed many confusing and/or rarely-used features e.g.,


explicit pointers, operator overloading etc.

No need to remove unreferenced objects because there is


Automatic Garbage Collection in java.

Object-oriented
Object-oriented means we organize our software as a combination
of different types of objects that incorporates both data and
behavior.

Object-oriented programming (OOPs) is a methodology that


simplifies software development and maintenance by providing
some rules.

Basic concepts of OOPs are:

1. Object
2. Class
3. Inheritance
4. Polymorphism
5. Abstraction
6. Encapsulation
Platform Independent

A platform is the hardware or software environment in which a program runs.

There are two types of platforms software-based and hardware-based. Java


provides software-based platform.

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

Java is secured because:

o No explicit pointer
o Java Programs run inside virtual machine sandbox

o Classloader: adds security by separating the package for the


classes of the local file system from those that are imported
from network sources.
o Bytecode Verifier: checks the code fragments for illegal code
that can violate access right to objects.
o Security Manager: determines what resources a class can access
such as reading and writing to the local disk.

These security are provided by java language. Some security can also
be provided by application developer through SSL, JAAS,
Cryptography etc.

Robust

Robust simply means strong. Java uses strong memory management.


There are lack of pointers that avoids security problem. There is
automatic garbage collection in java. There is exception handling and
type checking mechanism in java. All these points makes java robust.

Architecture-neutral

There are no implementation dependent features e.g. size of primitive


types is fixed.

In C programming, int data type occupies 2 bytes of memory for 32-bit


architecture and 4 bytes of memory for 64-bit architecture. But in
java, it occupies 4 bytes of memory for both 32 and 64 bit
architectures.

Portable

We may carry the java bytecode to any platform.

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

A thread is like a separate program, executing concurrently. We can


write Java programs that deal with many tasks at once by defining
multiple threads. The main advantage of multi-threading is that it
doesn't occupy memory for each thread. It shares a common memory
area. Threads are important for multi-media, Web applications etc.
4. Control Statements – Conditional Statements
Java If-else Statement

The Java if statement is used to test the condition. It checks boolean


condition: true or false. There are various types of if statement in
java.
o if statement (one way)

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
}

Java Switch Statement

The Java switch statement executes one statement from multiple


conditions. It is like if-else-if ladder statement.

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;
}

***case values can be integer constant, character constant and


string constant.
public class Example
{
public static void main(String[] args)
{
int number=20;
switch(number)
{
case 10: System.out.println("CSE");break;
case 20: System.out.println("EEE");break;
case 30: System.out.println("ECE");break;
default:System.out.println("Invalid input");
}
}
}

Java Switch Statement is fall-through

The java switch statement is fall-through. It means it executes all


statement after first match if break statement is not used with switch
cases.
public class Example2

{
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

public class EvenOdd


{
public static void main(String[] args)
{
int a =10;

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

public class Grade


{
public static void main(String[] args)
{
int m1= 70 , m2=62, m3=67;

float avg = (m1+m2+m3)/3;

if(avg >= 70)


System.out.println(" Distinction");
else if(avg >=60 && avg <70)
System.out.println(" First Class");
else if(avg >=50 && avg < 60)
System.out.println(" Second Class");
else if(avg >= 40 && avg <50)
System.out.println(" Third Class");
else
System.out.println(" Fail");
}
}
//Program to Find arithmetic operations using switch

public class Arithmetic


{
public static void main(String[] args)
{
int a=17, b=5 , c=0;
String choice="MUL";

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");
}

System.out.println("The result is " +c);

}
}

You might also like