0% found this document useful (0 votes)
11 views43 pages

Dr. Manaswita Saikia

This document provides an overview of Java programming fundamentals, including its origins, key features, and the structure of Java programs. It explains concepts such as Java's portability, security, and the role of the Java Virtual Machine (JVM) in executing bytecode. Additionally, it covers basic Java syntax, including data types, control statements, and the use of comments.

Uploaded by

samaira6005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views43 pages

Dr. Manaswita Saikia

This document provides an overview of Java programming fundamentals, including its origins, key features, and the structure of Java programs. It explains concepts such as Java's portability, security, and the role of the Java Virtual Machine (JVM) in executing bytecode. Additionally, it covers basic Java syntax, including data types, control statements, and the use of comments.

Uploaded by

samaira6005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Dr.

Manaswita Saikia
[email protected]
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 Buzzwords
 Simple
 easy to learn, inherits c/c++ syntax, left out difficult
constructs like pointers, memory management.
 Pure Object oriented Language-
 classes and objects
 Java is case sensitive language-
 variable val is different from Val, VAL, VaL
 Robust-
 Ability to create robust, reliable programs
• Exception handling
• Strictly typed language
( checks for errors at both compile and run time)
• Easy memory management
( automatic deallocation-garbage collection)
Java Buzzwords
 Multi threaded
 write programs that do many things
simultaneously

 Architecture neutral
 compiled java programs will run on variety of
processors using various OSs (The goal- “ Write
once, run any where, any time, forever”)

 Both compiled and interpreted


 enables creation of cross-platform programs
Java Buzzwords
 Dynamic
 Resolves type information at run time

 Distributed
 Designed for distributed environment of internet
because it handles TCP/IP protocol, RMI

 Security
 Execution is confined to JVM
Java and Internet
 Java can be used to create two types of
programs: applications and applets

 An application is a program
 that runs on your computer, under the
operating system of that computer
 An applet is a tiny Java program
 designed to be transmitted over the Internet
 automatically downloaded across the network,
just like an image, sound file, or video clip
 executed by a Java-compatible Web browser
Applets
• Applets
• Allow some functionality to be moved from server
to client like
• display data provided by the server
• handle user inputs
• provide simple functions ( loan
calculator)
that execute locally rather than on server

• Challenges: Introduces security and portability


issues
SECURITY
 Automatic downloading of applets might
contain virus
 malicious code gains unauthorized access to
system resources, passwords, credit card
numbers etc.
 But, when a Java-compatible Web browser
used,
 One can safely download Java applets
without fear of viral infection or malicious
intent
 Java achieves this protection
 by confining a Java program to the Java
execution environment and
Portability
• Portability is the major aspect of Internet
• because many different types of computers and
OSs are connected to it.
• In case of applets,
• must be able to downloaded and must be
executed by wide variety of computers and
OSs.
• Because Java is architecture neutral,
• Java programs are portable.
• They can be run on any platform without being
recompiled.
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
15
Java Compilers and the JVM
• Java compilers do not generate machine code for a CPU.
• Java compilers generate machine code for the JVM (Java
Virtual Machine).
• The JVM machine code (called bytecode) is executed by
a JVM interpreter program on each computer.

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


rights reserved.
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
First Simple Java Program
/*
This is a simple Java program.
Call this file Example.java.
*/
public class Example {
// A Java program begins with a call to main().
public static void main(String[] args) {
System.out.println("Java drives the Web.");
}
}

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


rights reserved.
• When a class member is preceded by public,
then that member may be accessed by code
outside the class in which it is declared

• The keyword static allows main( ) to be


called without having to instantiate a
particular instance of the class

• The keyword void simply tells the compiler


that main( ) does not return a value
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 assert 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