0% found this document useful (0 votes)
31 views32 pages

Lecture-4 5

Kushagra Agrawal

Uploaded by

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

Lecture-4 5

Kushagra Agrawal

Uploaded by

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

UNIVERSITY INSTITUTE OF ENGINEERING

DEPARTMENT OF AIT - CSE


Bachelor of Engineering (CSE)
Programming in Java (21CSH-244)
By: Kushagra Agrawal(E13465)

Lecture – 4 & 5
DISCOVER . LEARN . EMPOWER
JVM, Data types, Variables
Chapter Course Objectives

● To understand the about of JAVA Virtual Machine


1.
● To understand the about of Data Types and Variables

Chapter Course Outcomes


After completion of this course, student will be able to

● Aware the features of JVM


1. ● Able to know java rich data types and variables

2
Contents
• Introduction to JAVA program.
• About JVM.
• JAVA Keywords
• JAVA Variables and Data types.

3
A First Simple Program

4
Overview of main() function

5
The Java Virtual Machine

• JVM is the heart of the entire java program execution process.

• First,JVM loads the .class file into memory.

• It verifies whether all byte code instructions are proper or not. if it


finds any instruction suspicious ,the execution is rejected immediately.

• If the byte instructions are proper, then It allocates necessary memory


to execute the program.
How it works…!
Compile-time Environment Run-time Environment

Class Loader
Java Class
Bytecode Libraries
Verifier
Java
Source
(.java)

Java Java Just in Time


Bytecodes Interpreter Compiler Java
move locally Virtual
or through machine
Java network
Compiler
Runtime System

Java Operating System


Bytecode
(.class )
Hardware
JDK,JRE,JVM
Whitespace
• Java is a free-form language.
• This means that you do not need to follow any special indentation
rules.
• Program could have been written all on one line or in any other strange
way you felt like typing it.
• As long as there was at least one whitespace character between each
token that was not already delineated by an operator or separator.
• In Java, whitespace is a space, tab, or newline.
Identifiers
• Identifiers are used for class names, method names, and variable names.
• An identifier may be any descriptive sequence of uppercase and lowercase letters,
numbers, or the underscore and dollar-sign characters.
• They must not begin with a number, lest they be confused with a numeric literal.
• Java is case-sensitive, so VALUE is a different identifier than Value.
• Some examples of valid identifiers are:
• AvgTemp
• count a4
• $test
• this_is_ok
• Invalid identifier names include these:
2count
high-temp
Not/ok
Literals
• A constant value in Java is created by using a literal representation of it.
For example, Here are some literals:
• 100
• 98.6
• 'X'
• "This is a test"
Comments
• Java supports three types of comments.
• The first two are the // and the /* */.
• The third type is called a documentation comment.
• It begins with the character sequence /**. It ends with */.
• Documentation comments allow you to embed information about your program
into the program itself(HTML files).
Separators
The Java Keywords
• There are 50 keywords currently defined in the Java language .
• These keywords cannot be used as names for a variable, class, or method.

• The keywords const and goto are reserved but not used.
• In addition to the keywords, Java reserves the following: true, false, and null.
These are values defined by Java. You may not use these words for the names of
variables, classes, and so on.
JAVA Variables
• The variable is the basic unit of storage in a Java program.
• A variable is defined by the combination of an identifier, a type, and an optional
initializer.
• In addition, all variables have a scope, which defines their visibility, and a lifetime.
Declaring a Variable
• In Java, all variables must be declared before they can be used.
• The basic form of a variable declaration is shown here:
• type identifier [ = value][, identifier [= value] ...] ;
• int n=2,m=3;
Variables

• Variable is a name of memory location.


• There are three types of variables in java: local, instance and static.

• Local Variable
A variable which is declared inside the method is called local variable.
• Instance Variable
A variable which is declared inside the class but outside the method, is called instance variable . It
is not declared as static.
• Static variable
A variable that is declared as static is called static variable. It cannot be local.
Data Types in Java
Default values
Integers
• Java defines four integer types: byte, short, int, and long.
• All of these are signed, positive and negative values.

byte:
• Byte variables are declared by use of the byte keyword.
• This is a signed 8-bit type that has a range from –128 to 127.
• For example, the following declares two byte variables called b and c:
• byte b, c;
Integers(cont..)
short
• short is a signed 16-bit type.
• It has a range from –32,768 to 32,767.
• It is probably the least-used Java type. Here are some examples of short variable
declarations:
• short s;
• short t;
int
• The most commonly used integer type is int.
• It is a signed 32-bit type that has a range from –2,147,483,648 to 2,147,483,647.
• int n;
• int m;
Integers(cont..)
long
• long is a signed 64-bit type and is useful for those occasions where an int type is
not large enough to hold the desired value.
• long n;
• long m;
Ex:
• class Simple{
• public static void main(String[] args){
• int a=10;
• int b=10;
• int c=a+b;
• System.out.println(c);
• }}
Floating-Point Types
• Floating-point numbers, also known as real numbers, are used when evaluating
expressions that require fractional precision.

float
• The type float specifies a single-precision value that uses 32 bits of storage.
• float hightemp, lowtemp;
double
• Double precision, as denoted by the double keyword, uses 64 bits to store a
value.
• double pi, r, a;
Characters
• In Java, the data type used to store characters is char.
• Java char is a 16-bit type.
• The range of a char is 0 to 65,536.
• There are no negative chars.
• Java uses Unicode to represent characters. Unicode defines a fully international
character set that can represent all of the characters found in all human
languages.

• char ch1, ch2;


Booleans
• Java has a primitive type, called boolean, for logical values.
• It can have only one of two possible values, true or false.

• boolean b;
Type Conversion and Casting
Java’s Automatic Conversions
• When one type of data is assigned to another type of variable, an automatic type conversion
will take place if the following two conditions are met:
• The two types are compatible.
• The destination type is larger than the source type.
Casting Incompatible Types
• To create a conversion between two incompatible types, you must use a cast. A cast is simply
an explicit type conversion. It has this general form:
• (target-type) value
• int a;
• byte b;
• b = (byte) a;
Example
• // Demonstrate casts.
• class Conversion {
• public static void main(String args[]) {
• byte b;
• int i = 257;
• double d = 323.142;
• System.out.println("\nConversion of int to byte.");
• b = (byte) i;
• System.out.println("i and b " + i + " " + b);
• System.out.println("\nConversion of double to int.");
• i = (int) d;
• System.out.println("d and i " + d + " " + i);
• System.out.println("\nConversion of double to byte.");
• b = (byte) d;
• System.out.println("d and b " + d + " " + b);
• }}
Example(cont..)
O/P:
• This program generates the following output:
• Conversion of int to byte.
• i and b 257 1
• Conversion of double to int.
• d and i 323.142 323
• Conversion of double to byte.
• d and b 323.142 67
Summary
• Discussed First java program.
• Discussed JVM,JDK and JRE.
• Discussed keywords, Variables and Data types

29
Home Work
Q1. Why java is rich data types?

Q2. How many Translators are used in JAVA ? Explain.

30
References

Online Video Link


• https://nptel.ac.in/courses/106/105/106105191/
• https://www.coursera.org/courses?query=java
• https://www.coursera.org/specializations/object-oriented-programming
• https://www.youtube.com/watch?v=aqHhpahguVY

Text Book
• Herbert Schildt (2019), “Java The Complete Reference, Ed. 11, McGraw-Hill .
• “Head First Java” by Kathy Sierra & Bert Bates O’Reilly Publication

31
THANK YOU

For queries
Email: [email protected]

You might also like