Date post: | 25-May-2015 |
Category: |
Documents |
Upload: | fraz-bakhsh |
View: | 127 times |
Download: | 0 times |
Java in two semesters by Quentin Chratan and Aaron Kans
public class HelloObject Oriented languages require that program is to
be written in separate units called classesTelling the compiler that we are writing a class named
as HelloPublic indicates that we are placing this class
accessible to every oneA public class should always be saved in file with the
same name as of the classSo what will be name of our program??
Hello.javaOur program will interact with multiple built in java
libraries classes
{ } 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
System.out.println(“Hello World”);Display “Hello World”println is short for “print line”
; Java instruction has to end with a semi colon.
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
Java InstructionComputer Memory
int score;
char level;
int score, hits;
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
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
int score = 0; int score;score =0;
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 quotesAssignment at the time of declaration
level =‘B’Assignments changed
Data items in program have values that do not change
Following are examples of such itemsMaximum score in a examNumber of hours in a dayMathematical 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
Operation Java Operator
Addition +Subtraction -Multiplication *Division /Remainder %
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);
X = X + 1;X++;
X = X – 1;X--;
Postfix Prefix
Y=X++; Y=++X;
Y value will be 5X value will be 6
Y value will be 6X value will be 6
int X = 5;int Y = 0;
Postfix Prefix
Y=X--; Y=--X;
Y value will be 5X value will be 4
Y value will be 4X value will be 4
•Y = Y + X;•Y + = X;
System.out.println(“Hello World”); System.out.println(“Welcome to java world”);
Hello WorldWelcome 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
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
Complex data typeNameAddressCar Registration NumberAny meaningless sequence of characters
Declare it in the same was as declare variablesString Name;Name = “Saira”;ORString Name = “Saira”;
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 utilA 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
Create Object Scanner sc = new Scanner(System.in);
System.in represent KeyBoardnew 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);