Lecture 3 OOP (Java Syntax)
Lecture 3 OOP (Java Syntax)
System.out.println("Hello World!"); }
}
COMPILING AND RUNNING
THE PROGRAM
• Java Systems
• Consist of environment, language, Java Applications
Programming Interface (API)
• Java programs have five phases
1. Edit
• .java extension
2. Compile
• javac command: javac MyProgram.java
• Creates .class file containing bytecodes with similar name
JAVA PROGRAM DEVELOPMENT AND EXECUTION
STEPS…
3. Loading
• Class loader transfers .class file into memory
• Classes loaded and executed by interpreter with java
command
• To load,
appletviewer Welcome.html
OR
java MyProgram
JAVA PROGRAM DEVELOPMENT AND EXECUTION
STEPS…
4. Verify
• Bytecode verifier makes sure bytecodes are valid and do
not violate security
5. Execute
• Computer interprets program one bytecode at a time
• Performs actions specified in program
Program is created in
Phase 1 Editor Disk the editor and stored
on disk.
Compiler creates
Phase 2 Compiler Disk bytecodes and stores
them on disk.
Primary
Memory
Class Loader
Phase 3
Class loader puts
bytecodes in memory.
Disk
..
..
..
Primary
Memory
Bytecode Verifier
Phase 4 Bytecode verifier
confirms that all
bytecodes are valid
and do not violate
Java’s security
.. restrictions.
..
..
Primary
Interpreter Memory Interpreter reads
Phase 5 bytecodes and
translates them into a
language that the
computer can
understand, possibly
.. storing data values as
.. the program executes.
..
UNDERSTANDING BASICS
• Naming Conventions
• MyClass
• myMethod()
• myVariable
• MY_CONSTANT
PERFORMING BASIC
TASKS
IN JAVA
TOPICS
• Things to Remember
• Taking in command line arguments
• Primitives vs. Objects
• Wrapper classes and Conversions
• Taking Input and Output using Swing
• Selection and Control Structures
• OOP in java (Defining and using class)
THINGS TO REMEMBER
• Name of file must match name of class
• It is case sensitive
• About main()
• “main” is the function from which your program
starts
• Why public?
• So that run time can call it from outside
• Why static ?
• it is made static so that we can call it without creating an
object
• String concatenated with any other data type such as int will
also convert that datatype to String and the result will be a
concatenated String displayed on console
• For Example
• int i = 4
• int j = 5 ;
• System .out.println (“Hello” + i) // will print Hello 4 on screen
• However
• System,.out..println( i+j) ; // will print 9 on the console
System.out.println("Hello" + i);
System.out.println(i + j);
if (s1 == s2) {
System.out.println(“comparing string using == operator”);
}
if (s1.equals( s2) ) {
System.out.println(“comparing string using equal method”);
}
}
COMPILE AND EXECUTE
TAKING IN COMMAND
LINE ARGUMENTS
TAKING IN COMMAND LINE
ARGUMENTS
/* This program will take two arguments Hello World from the
command prompt and prints them to standard console. If you
specify less than two arguments an exception will be thrown */
// The “+” operator here works similar to “<<“ operator in C++. This line
is // equivalent to
cout<<“Arguments:”<<i<<“value”<<args[i];
// where cout is replaced by System.out.println, and “<<“ is
replaced by + for // concatenation
• Primitive data types are generally used for local variables, parameters and
instance variables (properties of an object)
• Primitive datatypes are located on the stack and we can only access their
value, while objects are located on heap and we have a reference to these
objects
• Also primitive data types are always passed by value while objects are
always passed by reference in java. There is no C++ like methods
STACK VS. HEAP
Stack Heap
public static void main(String args[])
{
int num= 5; num
5
Student st = new Student();
} 0F59
name
ali
st
0F59
PRIMITIVES (CONT)
• For all built-in primitive data types java uses lowercase. E.g int , float
etc
• You can get a primitive data type from a Wrapper using the
corresponding value function
• int primNum = num.intValue();
STACK VS. HEAP
Stack Heap
public static void main(String args[])
{
int num= 5; num
5
Integer numObj = new Integer (10);
}
04E2
10
numObj
04E2
WRAPPER USES
• Defines useful constants for each data type
• For example,
Integer.MAX_VALUE
• Boxing/Unboxing Conversions
• New feature added in j2se 5.0
• Boxing
• Integer iWrapper = 10;
• Prior to J2SE 5.0, we use
• Integer a = new Integer(10);
• Unboxing
• int iPrimitive = iWrapper;
• Prior to J2SE 5.0, we use
• int b = iWrapper.intValue();
INPUT / OUTPUT
CONSOLE BASED OUTPUT
SYSTEM.OUT
• System class
• Out represents the screen
• System.out.println()
• Prints the string followed by an end of line
• Forces a flush
• System.out.print()
• Does not print the end of line
• Does not force a flush
• System.out.flush()
• Force a flush
INPUT / OUTPUT
/* This program will takes the input (number) through GUI and prints its square on the console as
well as on the GUI. */
import javax.swing.*;
• &&, ||
• Logical AND, OR. Both use short-circuit evaluation to
more efficiently compute the results of complicated
expressions.
•!
• Logical negation.
SWITCH SELECTION
STRUCTURE
import javax.swing.*;
public class SwitchTest {
// continue….
SWITCH SELECTION
STRUCTURE…
switch(ch)
{
case 1:
int sum = operand1 + operand2;
System.out.println(“sum: ” + sum );
break;
case 2:
int product = operand1 * operand2;
System.out.println(“product: ” + product );
break;
default:
System.out.println(“wrong choice!”);
System.exit(0);
}
}
COMPILE AND EXECUTE
CONTROL STRUCTURES
FOR, WHILE & DO-WHILE
LOOPING CONSTRUCTS
• while
while (continueTest) {
body;
}
• do
do {
body;
} while (continueTest);
// ^ don’t forget semicolon
• for
for(init; continueTest; updateOp) {
body;
}
CONTROL STRUCTURES
public class ControlStructTest {
// for loop
for (int i=1; i<= 5; i++) {
System.out.println("hello from for");
}
// while loop
int j = 1;
while (j <= 5) {
System.out.println("Hello from while");
j++;
}