Lecture-4 5
Lecture-4 5
Lecture – 4 & 5
DISCOVER . LEARN . EMPOWER
JVM, Data types, Variables
Chapter Course Objectives
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
Class Loader
Java Class
Bytecode Libraries
Verifier
Java
Source
(.java)
• 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
• 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.
• 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?
30
References
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]