0% found this document useful (0 votes)
8 views

object oriented programming

The document outlines guidelines for class attendance and participation, emphasizing the importance of preparation and record-keeping. It provides an overview of Java programming, including its origins, bytecode, and the Java Development Kit (JDK). Additionally, it includes examples of Java code, control structures, and exercises for understanding Java concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

object oriented programming

The document outlines guidelines for class attendance and participation, emphasizing the importance of preparation and record-keeping. It provides an overview of Java programming, including its origins, bytecode, and the Java Development Kit (JDK). Additionally, it includes examples of Java code, control structures, and exercises for understanding Java concepts.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Dr.

Manjula Shenoy K
Manju.Shenoy@manipal.edu
9880651687
Do’s and don’t’s
• For attendence to be awarded, clearly answer when
your name is called. Later updation is not entertained.
• No wash room, No drinking water request etc allowed.
• Participation in class discussion about the subject when
asked for is appreciated and any help/benefit from my
side depends on this.
• Come prepared for the Lab as per Lab manual
explanation
• Observation of 200 pages record book mandatory
• Notes to be maintained in theory class
Chapter 1
Java Programming
Fundamentals

Copyright © 2013 by The McGraw-Hill Companies, Inc. All


rights reserved.
The Origins of Java

• Java is a high-level language conceived by James


Gosling in 1991.
• Initially called “Oak”, but was renamed “Java” in
1995.

• Java syntax is similar to the older languages C &


C++.
• It was not designed to replace C++.

• Original motivation – need for a platform-


independent language for creating s/w to be
embedded in various consumer electronic devices
/ Internet
•Copyright
The©newest version is Java SE 23.
2013 by The McGraw-Hill Companies, Inc. All
rights reserved.
Progress Check
• Java is useful for the Internet because it can be used to produce
______ programs
• Java is the direct descendant of what languages?
Java’s solution:

 Execution of bytecode by JVM


 is the easiest way to create portable programs by
implementing java interpreter for each platform.
Java bytecode
 The output of a Java compiler(javac) is
 not executable code ; rather, it is bytecode

 Bytecode is a highly optimized set of instructions


 designed to be executed by the Java run-time system, Java
Virtual Machine (JVM)
Java Virtual Machine(JVM)
A Picture is Worth…
The output of the
compiler is .class
file

The Interpreter's are sometimes referred to as Java Virtual


Machines
16
Just –in-time Compiler(JIT)
 When JIT is part of JVM
 Selected portions of bytecode are compiled into executable
code at run time
 On a piece-by-piece, demand basis
 It does not compile an entire java program
 Furthermore, not all sequences of bytecode are compiled
 only those that will benefit from compilation
Java Runtime Environment (JRE)

• a set of programming tools for developing


Java applications:
• Java Virtual Machine (JVM),
• core classes ( libraries),
• supporting files.
Java Development Kit(JDK)
Progress Check
• What is an applet?
• What is a Java bytecode?
• Which two internet programming problems can be solved using
bytecode?
The Java Development Kit
(JDK)
• To compile and run java programs JDK must be
installed in the m/c, which can be downloaded using
the below link
• www.oracle.com/technetwork/java/javase/
downloads/index.html
• Provide two primary programs
Java compiler: javac
Application launcher or java interpreter :
java
•It runs in the command prompt environment
and use command line tools
Progress Check
• Where does a Java program begin execution?
• What does System.out.println() do
• What is the name of the java compiler? What do you use to run java
program?
• Why main is declared as public?
• Why main is declared as static?
• If I run a Program in commandline as java Example 1 2 3 what is args[0]?
• What is the command to compile java in commandline?
• Name some primitive data types you studied in java.
• What is bytecode? What extension of file name it has?
• Write a Program to convert 10 gallons to liters.(1Gallon=3.7854l)
A Second Example
class Example2 {
public static void main(String[] args) {
int var1; // this declares a variable
int var2; // this declares another variable

var1 = 1024; // this assigns 1024 to var1


System.out.println("var1 contains " + var1);

var2 = var1 / 2;
System.out.print("var2 contains var1 / 2: ");
System.out.println(var2);
}
}
Copyright © 2013 by The McGraw-Hill Companies, Inc. All
rights reserved.
A Third Example
class Example3 {
public static void main(String[] args) {
int w; // declare an int variable
double x; // declare a floating-point variable

w = 10; // assign w the value 10


x = 10.0; // assign x the value 10.0
System.out.println("Original value of w: " + w);
System.out.println("Original value of x: " + x);
System.out.println(); // print a blank line

// now, divide both by 4


w = w / 4;
x = x / 4;
System.out.println("w after division: " + w);
System.out.println("x after division: " + x);
}
}Copyright © 2013 by The McGraw-Hill Companies, Inc. All
rights reserved.
Converting Gallons to Liters
class GalToLit {
public static void main(String[] args) {
double gallons; // holds the number of gallons
double liters; // holds conversion to liters

gallons = 10; // start with 10 gallons

liters = gallons * 3.7854; // convert to liters


System.out.println(gallons + " gallons is " +
liters + " liters.");
}
}

Copyright © 2013 by The McGraw-Hill Companies, Inc. All


rights reserved.
The if Statement
• Simplest form:
if ( condition ) statement;

• Example:
if(3 < 4) System.out.println("yes");

• Relational operators:
<, >, <=, >=, ==, !=

Copyright © 2013 by The McGraw-Hill Companies, Inc. All


rights reserved.
Example of if Statements
class IfDemo {
public static void main(String[] args) {
int a, b;
a = 2;
b = 3;

if(a < b)
System.out.println("a is less than b");

// this won't display anything


if(a == b)
System.out.println("you won't see this");
}
}
Copyright © 2013 by The McGraw-Hill Companies, Inc. All
rights reserved.
Code Block
• A list of statements inside braces
• Does not need to end in a semicolon

• Example:
if(w < h) {
v = w*h;
w = 0;
}

Copyright © 2013 by The McGraw-Hill Companies, Inc. All


rights reserved.
Comments in Java
• Java supports single line and multi-line
comments very similar to C++.

Example 1
//This is an example of single line comment

Example 2
/*This is my first java program.
This will print 'Hello World‘
as the output
*/
Indentation Practices
• The Java compiler doesn’t care about
indentation.

• Use indentation to make your code more


readable.
• Indent one level for each opening brace and
• move back out after each closing brace.

Copyright © 2013 by The McGraw-Hill Companies, Inc. All


rights reserved.
Progress Check
• How is a block of code created? What does it do?
• In Java statements are terminated by a ____
• All Java statements must start and end on one line. T/F
Java Keywords

abstract asset boolean break byte case catch char class const

continue default do double else enum extend final finally float


s

for goto if implement impor instanceof int interfac long nativ


s t e e

new package private protected public return short static strictfp super

switch synchronized this throw throw transient try void volatil while
s e

const and goto are reserved but not used.


Java Identifiers
• An identifier is a name given to
• a method, variable, class or other user-defined item.

• Identifiers are one or more characters long.

• The dollar sign($), the underscore(-), any letter of


the alphabet(a-z,A-Z), and any digit(0-9) can be
used in identifiers.

• Should not be a keyword or reserved word


Copyright © 2013 by The McGraw-Hill Companies, Inc. All
rights reserved.
Java Identifiers
• The first character in an identifier cannot be a
digit.
• e.g. 12a is an invalid identifier

• Java is case sensitive: Upper case and lower


case are different.
• myvar and MyVar are different identifiers.

• Legal identifiers
• Test , x, y2, maxLoad, sample34, $up, _top
Java class Libraries
• A package which gets imported
automatically to all the java programs
• ‘java.lang’

• java.lang’ package has


• many built-in classes and methods ( System
and String classes) and
• println and print methods.
• Different packages for
• I/O , Applets , GUI, Event handling, networking,
multithreading, exception handling etc.
Exercises
• Which of the following variable names is invalid?
• A. count
• B. $count
• C. count27
• D. 67count

• What is wrong with each of the following commands


• javac Example.class
• Java Example.class
Exercises
• Assume x is a variable that is declared as type int. What is wrong
with each of the following statements?
• A. x=3.5;
• B. if(x=3) x=4;
• C. x= “34”

You might also like