The document provides an introduction to object-oriented programming concepts in Java including classes, methods, and variables. It explains how to write a simple "Hello World" program in Java with a main method and how to declare and initialize variables of different data types. It also covers basic input/output using print statements and the Scanner class.
1. Java in two semesters by Quentin Chratan and Aaron Kans
2. public class Hello
Object Oriented languages require that program is to be
written in separate units called classes
Telling the compiler that we are writing a class named
as Hello
Public indicates that we are placing this class accessible
to every one
A public class should always be saved in file with the
same name as of the class
So what will be name of our program??
Hello.java
Our program will interact with multiple built in java
libraries classes
3. {}
Everything in class has to be contained between two curly
brackets (Braces) to indicate the beginning and end of class
Java is case sensitive
Upper case letters and lower case letters are different
public static void main(String[] args)
Every program will have at least one class with this line
This is method
Main is a special method, this is where the program begins
A program starts with the first instruction of main and then
obeys each instruction in sequence.
The program terminates when it has finished obeying the final
instruction of main
{ } indicates the body of main and marks its starting and
ending
4. System.out.println(“Hello World”);
Display “Hello World”
println is short for “print line”
;
Java instruction has to end with a semi colon.
5. Although you can choose any name such as x, its best to pick a
name that describe the purpose of data item
E.g.
A computer game might need a piece of data to record the
player’s score as secret key.
What should be the name of variable to store such data
Score
What should be the data type of such variable (byte, short, int
and long)
int
You can choose any name for variable as long as
The name is not already a word in java language (class, void,
public)
Name has no spaces in it
Name does not include operators or mathematical symbols such
as + or –
Name starts with either a letter, an underscore (_) or a dollar sign
($)
You can use any letter as starting letter but java convention is to
begin the name with lower case letter
7. Allows values to be put into variables
Written with equality symbol (=)
Assignment Operator
variableName = value;
score =0;
Set value of score to zero OR score becomes equal to zero
Puts the number zero into memory we called score
Combine variable statement with a variable declaration
int score = 0; int score;
score =0;
What will be the effect of following statement in java?
int score = 2.5
This statement will not compile as 2.5 is a real number
8. double something =1000;
Though 1000 is an integer but this statement is legal as
will not result in information loss. It will be considered
as 1000.0
Character Assignment
char level = ‘A’;
Enclosed he character in single quotes
Assignment at the time of declaration
level =‘B’
Assignments changed
9. Data items in program have values that do not change
Following are examples of such items
Maximum score in a exam
Number of hours in a day
Mathematical value of (3.14176)
In these cases the values do not vary
Values remain constant
Such items should be named and declared as
constants
Add the keyword “final” before declaration
final int HOURS = 24
The statement HOURS = 12 will not compile now
11. int x;
x =10 + 25;
double cost;
cost = 500*(1+17.5/100);
int x;
x=30/4;
What Will be the answer? 7 or 7.5?
The answer is 7 as division operator is overloaded
double price, tax, cost;
price = 500;
tax =17.5;
cost =price+(1+tax/100);
price =price+(1+tax/100);
12. X = X + 1;
X++;
X = X – 1;
X--;
int X = 5;
int Y = 0;
Postfix Prefix
Y=X--;
Y=X++; Y=--X;
Y=++X;
Y value will be 5 Y value will be 6
4
X value will be 6
4 X value will be 6
4
•Y = Y + X;
•Y + = X;
13.
14.
15. System.out.println(“Hello World”);
System.out.println(“Welcome to java world”);
Hello World
Welcome to java world
System.out.print(“Hello World”);
System.out.println(“Welcome to java world”);
Hello WorldWelcome to java world
System.out.println();
Blank line in the program
16. Strings
Collection of characters
Always enclosed in speech marks “ “
Print statements print strings
Several strings can be combined using + operator
Concatenation Operator (+)
System.out.println(“Hello” + “World”);
HelloWorld
Spaces included in speech marks are printed
System.out.println(“Hello ” + “World”);
Hello World
System.out.println(10*10);
The instruction prints 100 on screen.
Java converts value/expression to a string before displaying it
As these numbers are converted into string so they can be
combined
System.out.println(“Cost = ”+(10*10));
Cost = 100
17. Complex data type
Name
Address
Car Registration Number
Any meaningless sequence of characters
Declare it in the same was as declare variables
String Name;
Name = “Saira”;
OR
String Name = “Saira”;
18. Part of Java release 5.0 and later
Class that makes it easy for us to write a program
that obtains information that is typed in at the
keyboard
Scanner is part of Java package called util
A package is a collection of pre-compiled classes
To make Scanner class accessible to compiler, we
have to tell the compiler that it should look in util
package
import java.util.*;
* means all classes in util package are made available
import java.util.Scanner;
Only Scanner class is accessible
19. Create Object
Scanner sc = new Scanner(System.in);
System.in represent KeyBoard
new instantiates a class by allocating memory for a new object
and returning a reference to that memory.
Integer Input
int x;
x=sc.nextInt();
Double Input
double x;
x=sc.nextDouble();
String Input
String x;
x=sc.next(); OR x=sc.nextLine();
Character Input
char x;
x=sc.next().charAt(0);