0% found this document useful (0 votes)
36 views20 pages

Lecture 2

The document discusses writing a first Java program and covers topics like constants, data types, variables, literals, and the Java Runtime Environment and Java Development Kit. It provides examples of simple Java programs and discusses creating, compiling, and running Java programs.

Uploaded by

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

Lecture 2

The document discusses writing a first Java program and covers topics like constants, data types, variables, literals, and the Java Runtime Environment and Java Development Kit. It provides examples of simple Java programs and discusses creating, compiling, and running Java programs.

Uploaded by

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

LECTURE TWO

Writing First Java Program


&
Constant, Data Types and Variables
JAVA PROGRAM

Java Source
Code

Java Compiler

Applet Type Application Type

Java Enabled
Browser Java Interpreter

Output
Output
SIMPLE JAVA PROGRAM-EXAMPLE 1
/*This is a simple java program*/
class Example
{
public static void main (String args[])
{
System.out.println (“This is a simple Java program”);
}
}
SIMPLE JAVA PROGRAM-SOME
IMPORTANT POINTS
 public: Access specifier. main() must be made public,
since it must be called by code defined outside it’s class.
 Static: It is required because main() is called without
creating an object of it’s class
 String args[]: An array of objects of type String class.
Each object of type string contains a character string. It
is used to manipulate command line argument.
 Java is case sensitive.
 System predefined class that refers to system.
out It is static data member of System class
println() It is a member of out object
JRE AND JDK
 JRE: Java Runtime Environment
 provides
 libraries,

 Java virtual machine,

 other components necessary for you to run applets


and applications
 JDK: Java Development Kit
 includes
 JRE

 command-line development tools such as compilers


and debuggers
IMPLEMENTING A JAVA PROGRAM
1. Creating a java program
2. Compiling a java program
3. Running the program

Creating a Java Program:


1. All java source file has an extension .java.
2. If a program contains multiple classes, the file name
must be the class name of the class containing the
main method.
IMPLEMENTING A JAVA PROGRAM
Compiling a Java Program:
 To compile a java program, execute the java compiler
javac, specifying the name of the source file on the
command line. C:\> javac Example.java
 When java source code is compiled, each individual
class is put into it’s own output file named after the class
and using the .class extension.
 Each file with .class extension contains the bytecode
corresponding to that class
IMPLEMENTING A JAVA PROGRAM
 To run the program, interpreter java is used the name of
the class that contains the main function.
c:\> java Example
 Actually it searches for Example. class file.
SIMPLE JAVA PROGRAM – EXAMPLE 2
import java.lang.Math
class SquareRoot
{
public static void main (String args [])
{
double x=5, y;
y = Math.sqrt(x);
System.out.println(“y = “+y);
}
}
JAVA IS A STRONGLY TYPED LANGUAGE
 Every variable and expression has a strongly defined
type.
 All assignments are checked for type compatibility.
 Java compiler checks all expressions and parameters to
ensure that the types are compatible.
THE PRIMITIVE TYPES
 There are exactly eight primitive data types in Java
 Four of them represent whole valued signed numbers:
 byte, short, int, long
 Two of them represent floating point numbers:
 float, double
 One of them represents characters:
 char
 And one of them represents boolean values:
 boolean
NUMERIC PRIMITIVE TYPES
 The difference between the various numeric primitive
types is their size, and therefore the values they can
store:

Type Storage Min Value Max Value

byte 8 bits -128 127


short 16 bits -32,768 32,767
int 32 bits -2,147,483,648 2,147,483,647
long 64 bits < -9 x 1018 > 9 x 1018

float 32 bits
double 64 bits
CHAR
 It uses unicode to represent character.
 The char type is unsigned 16 bit values ranging from 0 to 65536.
 ASCII still ranges from 0 to 127.

Example:
class test
{
public static void main (String args[])
{
char ch1, ch2;
ch1=88;
ch2=‘Y’;
System.out.println (“ch1 and ch2: “ + ch1+” “+ch2);
}
}
Output: ch1 and ch2: X Y
CHAR
Example:
class test
{
public static void main (String args[])
{
char ch1;
ch1= ‘X’;
Sytem.out.println (“ch contains “+ch1);
ch1++;
System.out.println (“ch1 is now “ + ch1);
}
}
Output:
ch1 contains X
Ch1 is now Y
BOOLEANS
 Size is 1 bit – two value: true and false.
 This type is returned by all relational operators.
 Example:
boolean b;
b= true;
1. System.out.println(“b is “+b);
2. System.out.println(“10>9 is “ +(10>9));

Output:
b is true
10>9 is true
LITERALS
 Integer Literals
1. base 10 – 1,2,43 etc.
2. Any whole number is by default integer (32 bits).
• To specify a long literal, the number should appended with
an upper- or lowercase L.
LITERALS
 Floating point Literals
1. Standard Notation – 3.14159, 0.6667, 2.0 etc.
2. Scientific Notation – 6.022E23, 2e+100.
• Floating point literals are by default of type double.
• To specify a float literal, we must append an F or f to the
constant.
LITERALS
 Boolean Literals
• Two values – true and false.
• True is not equal 1 and false is not equal to 0.
• They can be assigned to variable declared as boolean.
LITERALS
 Character Literals:
 Can be converted into integers and manipulated with the integer operators.
• A literal character is represented inside a pair of single quotes.

Escape sequence Description

1. \’ Single quote
2. \” Double quote
3. \\ Backslash
4. \r Carriage Return
5. \n New line
6. \t Tab
7. \b Backspace
LITERALS
 String Literals
• A sequence of characters between a pair of double
quotes.
• In java string must begin and end on the same line.

You might also like