Day 3 - Object Oriented Programming
Day 3 - Object Oriented Programming
Programming
Structures in Java
Lecture - 02
March 12,2013
1
Outline
Basic Java Programming Steps
Operators
2
Basic Java Programming Steps
public class FirstSample
{
public static void main(String[] args)
{
System.out.println(“We will not use
Hello World”);
}
}
3
Analysis of Program Structure
Everything in a java program lives inside a class.
Class is like a container for your program.
4
Program Compilation and Execution
Points to Remember
1. You need to name your source code file the same as the
name of the public class with the extension .java
2. After successful compilation the Java Compiler
automatically names the byte code file FirstClass.class and
stores it in the same directory as the source file
3. During program execution using the keyword java on the
command line, the JVM always start executing your program
from the code in the main method in the class you indicate
5
Comments
Java has three kind of commenting styles available:
// use this to write single line comments
/*
/**
*/
The JDK contains a tool, called javadoc, that generates HTML
documentation from your source files.
6
Variables
Variables are places where information can be stored
while a program is running.
Their values can be changed at any point over the
course of a program
To create a variable, declare its name and the type of
information that it will store.
The type is listed first, followed by the name.
Example:
int highScore ;
type name
7
Variables: Naming Convention
The name you choose for your java variable is called
identifier.
The identifier must:
start with a letter, ($) dollar sign or (_) underscore
Be a sequence of letters or digits
Not contain +, @ or spaces
Not be a java reserved keyword
10
Integer Data Types
There are four data types that can be used to
store integers.
The one you choose to use depends on the
size of the number that you want to store.
Data Type Value Range
- byte smallValue;
smallValue = -55;
- int pageCount = 1250;
- long bigValue = 1823337144562L;
double g = 7.7e100 ;
double tinyNumber = 5.82e-203;
float costOfBook = 49.99F;
Example:
boolean printready = true;
boolean fileOpen = false;
Character Data Types
Character is a data type that can be used to store a
single characters such as a letter, number,
punctuation mark, or other symbol.
Example:
char firstLetterOfName = 'e' ;
char myQuestion = '?' ;
17
Introduction to String
Strings consist of a series of characters inside double
quotation marks.
Keyword final indicates that you can assign to the variable once,
Examples:
3 + 5 // uses + operator
14 + 5 – 4 * (5 – 3) // uses +, -, * operators
Arithmetic operators
Assignment operator
Increment/Decrement operators
Relational operators
Conditional operators
Arithmetic Operators
Java has 6 basic arithmetic operators
+ add
- subtract
* multiply
/ divide
% modulo (remainder)
^ exponent (to the power of )
double x = 63;
double y = 35;
System.out.println(x / y);
Ouput: 1.8
count = count - 1;
can be written as:
--count; or count--;
int numOranges = 5;
int numApples = 10;
int numFruit;
numFruit = ++numOranges + numApples;
numFruit has value 16
numOranges has value 6
The postfix form count++, count-- first evaluates the expression and then adds 1 to the
variable
int numOranges = 5;
int numApples = 10;
int numFruit;
numFruit = numOranges++ + numApples;
numFruit has value 15
numOranges has value 6
Relational (Comparison) Operators
• Relational operators compare two values
3) result = (x != x*y);
now result is assigned the value true because the product of
x and y (15) is not equal to x (3)
Conditional Operators
Symbol Name
&& AND
|| OR
! NOT
(x || y) evaluates to true
(true && x) evaluates to true
(x || y)
What happens if x is true?
Similarly, Java will not evaluate the right-hand operator y
if the left-hand operator x is true, since the result is
already determined in this case to be true.
POP QUIZ
1) What is the value of number? -12
int number = 5 * 3 – 3 / 6 – 9 * 3;
The main method does not operate any object. In fact, when a
The static main method executes, and constructs the objects that the
program needs.
Since main method is static JVM can call it without creating any
39