Java Introduction
Introduction
• Computer language innovation and
development occurs for two fundamental
reasons:
– To adapt to changing environments and uses
– To implement refinements and improvements in
the art of programming
• From C, Java derives its syntax.
• object-oriented features were influenced by
C++.
• The Birth of Modern Programming: C
– The creation of C was a direct result of the need
for a structured, efficient, high level language that
could replace assembly code when creating
systems programs.
– FORTRAN could be used to write fairly efficient
programs for scientific applications, it was not
very good for systems code
• Assembly language can be used to produce highly
efficient programs, but it is not easy to learn or
use effectively.
• Further, debugging assembly code can be quite
difficult.
• Invented and first implemented by Dennis Ritchie
on a DEC PDP-11 running the UNIX operating
system, C was the result of a development
process that started with an older language called
BCPL, developed by Martin Richards
• BCPL influenced a language called B, invented
by Ken Thompson, which led to the
development of C in the 1970s.
• C was formally standardized in December
1989, when the American National Standards
Institute (ANSI) standard for C was adopted.
• C is a successful and useful language, you
might ask why a need for something else
existed. The answer is complexity.
• The 1960s gave birth to structured programming
• even with structured programming methods,
once a project reaches a certain size, its
complexity exceeds what a programmer can
manage.
• a new way to program was invented, called
object-oriented programming (OOP)
• OOP is a programming methodology that helps
• organize complex programs through the use of
inheritance, encapsulation, and Polymorphism.
• C++ was invented by Bjarne Stroustrup in
1979, while he was working at Bell
• Laboratories in Murray Hill, New Jersey.
Stroustrup initially called the new language
“C with Classes.”
• in 1983, the name was changed to C++.
• C++ extends C by adding object-oriented
features.
• Java was conceived by James Gosling, Patrick
Naughton, Chris Warth, Ed Frank, and Mike
Sheridan at Sun Microsystems, Inc. in 1991
• This language was initially called “Oak” but
was renamed “Java” in 1995.
The Creation of Java
• An application is a program that runs on your
computer, under the operating system of that
computer.
• An applet is an application designed to be
transmitted over the Internet and executed by
a Java-compatible Web browser.
• an applet is a program that can react to user
input and dynamically change—not just run
the same animation or sound over and over
Java Applets and Applications
• every time that you download a “normal”
program, you are risking a viral infection.
• In addition to viruses, another type of
malicious program exists that must be
guarded against.
• Java answers both of these concerns by
providing a “firewall” between a networked
application and your computer.
• When you use a Java-compatible Web
browser, you can safely download
Security
• Many types of computers and operating
systems are in use throughout the world
• Many are connected to the Internet.
Portability
• Bytecode is a highly optimized set of
instructions designed to be executed by the
Java run-time system, which is called the Java
Virtual Machine (JVM).
• That is, in its standard form, the JVM is an
interpreter for bytecode.
• Translating a Java program into bytecode
helps makes it much easier to run a
• program in a wide variety of environments.
The Bytecode
• Simple
• Secure
• Portable
• Object-oriented
• Robust
• Multithreaded
The Java Buzzwords
•Architecture-neutral
•Interpreted
• High performance
• Distributed
• Dynamic
• Integers This group includes byte, short, int, and
long, which are for whole valued signed numbers.
• Floating-point numbers This group includes float and
double, which represent numbers with fractional
precision.
• Characters This group includes char, which
represents symbols in a character set, like letters
and numbers.
• Boolean This group includes boolean, which is a
special type for representing true/false values.
Data type
• Java does not support unsigned, positive-only
integers
Integers
• Floating-point numbers, also known as real
numbers, are used when evaluating
expressions that require fractional precision.
• All transcendental math functions, such as
sin(), cos( ), and sqrt( ), return double values.
Floating-Point Types
• Java uses Unicode to represent characters.
Unicode defines a fully international character
set that can represent all of the characters
found in all human languages.
• Thus, in Java char is a 16-bit type. The range
of a char is 0 to 65,536.
• There are no negative chars.
Characters
• Java has a simple type, called Boolean, for
logical values
• It can have only one of two possible values,
true or false.
• This is the type returned by all relational
operators
Booleans
• Java Is a Strongly Typed Language
• Part of Java’s safety and robustness every variable has a
type, every expression has a type, and every type is strictly
defined.
• Second, all assignments, whether explicit or via parameter
passing in method calls, are checked for type compatibility.
• There are no automatic coercions or conversions of
conflicting types as in some languages.
• The Java compiler checks all expressions and parameters to
ensure that the types are compatible.
• Any type mismatches are errors that must be corrected
before the compiler will finish compiling the class.
Java Is a Strongly Typed Language
• A variable is defined by the combination of an
identifier, a type, and an optional initializer.
• Declaring a Variable
– type identifier [ = value][, identifier [= value] ...] ;
• In Java, all variables must be declared before
they can be used.
Variables
• Java allows variables to be declared within any
block.
• A block defines a scope. Thus, each time you
start a new block, you are creating a new
scope.
• A scope determines what objects are visible to
other parts of your program.
• It also determines the lifetime of those
objects.
The Scope and Lifetime of
Variables
• When you declare a variable within a scope,
you are localizing that variable and protecting
it from unauthorized access
• Objects declared in the outer scope will be
visible to code within the inner scope.
• However, the reverse is not true. Objects
declared within the inner scope will not be
visible outside it. (SAMPLE WITH SWAP PROGRAM)
• If the two types are compatible, then Java will
perform the conversion automatically.
• it is still possible to obtain a conversion
between incompatible types.
• an automatic type conversion will take place if
the following two conditions are met:
– The two types are compatible.
– The destination type is larger than the source
type.
Type Conversion and Casting
• It has this general form:
– (target-type) value
• if the size of the whole number
• component is too large to fit into the target
integer type, then that value will be reduced
modulo the target type’s range.
– Conversion of int to byte.
– i and b 257 1
• First, all byte and short values are promoted
to int, as just described.
• Then, if one operand is a long, the whole
expression is promoted to long.
• If one operand is a float, the entire expression
is promoted to float.
• If any of the operands is double, the result is
double.
type promotion rules
• An array is a group of like-typed variables that
are referred to by a common name.
• Is a homogeneous data type
Arrays
• The general form of a one dimensional array
declaration is
– type array-var[ ];
• Here, type declares the base type of the array.
• the value of array is set to null
• new is a special operator that allocates
memory.
– array-var = new type[size];
One-Dimensional Arrays
• First, you must declare a variable of the
desired array type.
• Second, you must allocate the memory that
will hold the array, using new, and assign it to
the array variable.
• Thus, in Java all arrays are dynamically
allocated.
// Demonstrate a one-dimensional array.
class Array {
public static void main(String args[]) {
int month_days[];
month_days = new int[12];
month_days[0] = 31;
month_days[1] = 28;
month_days[2] = 31;
month_days[3] = 30;
month_days[4] = 31;
month_days[5] = 30;
month_days[6] = 31;
month_days[7] = 31;
month_days[8] = 30;
month_days[9] = 31;
month_days[10] = 30;
month_days[11] = 31;
System.out.println("April has " + month_days[3] + " days.");
}
}
• In Java, multidimensional arrays are actually
arrays of arrays.
• a two-dimensional array variable called twoD.
– int twoD[][] = new int[4][5];
Multidimensional Arrays
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String args[]) {
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
• If you need only specify the memory for the
first (leftmost) dimension.
• You can allocate the remaining dimensions
separately.
– int twoD[][] = new int[4][];
– twoD[0] = new int[5];
– twoD[1] = new int[5];
– twoD[2] = new int[5];
– twoD[3] = new int[5];
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++) {
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Output:
0
1 2
3 4 5
6 7 8 9
– type[ ] var-name;
• Eg:
– int al[] = new int[3];
– int[] a2 = new int[3];
• The following declarations are also equivalent:
– char twod1[][] = new char[3][4];
– char[][] twod2 = new char[3][4];
Alternative Array Declaration
Syntax
• Java does not support or allow pointers.
• Java does not support pointers that can be
accessed and/or modified by the programmer.
• Java cannot allow pointers, because doing so
would allow Java applets to breach the
firewall between the Java execution
environment and the host computer.
Pointers in Java

Java introduction

  • 1.
  • 2.
    Introduction • Computer languageinnovation and development occurs for two fundamental reasons: – To adapt to changing environments and uses – To implement refinements and improvements in the art of programming • From C, Java derives its syntax. • object-oriented features were influenced by C++.
  • 3.
    • The Birthof Modern Programming: C – The creation of C was a direct result of the need for a structured, efficient, high level language that could replace assembly code when creating systems programs. – FORTRAN could be used to write fairly efficient programs for scientific applications, it was not very good for systems code
  • 4.
    • Assembly languagecan be used to produce highly efficient programs, but it is not easy to learn or use effectively. • Further, debugging assembly code can be quite difficult. • Invented and first implemented by Dennis Ritchie on a DEC PDP-11 running the UNIX operating system, C was the result of a development process that started with an older language called BCPL, developed by Martin Richards
  • 5.
    • BCPL influenceda language called B, invented by Ken Thompson, which led to the development of C in the 1970s. • C was formally standardized in December 1989, when the American National Standards Institute (ANSI) standard for C was adopted. • C is a successful and useful language, you might ask why a need for something else existed. The answer is complexity.
  • 6.
    • The 1960sgave birth to structured programming • even with structured programming methods, once a project reaches a certain size, its complexity exceeds what a programmer can manage. • a new way to program was invented, called object-oriented programming (OOP) • OOP is a programming methodology that helps • organize complex programs through the use of inheritance, encapsulation, and Polymorphism.
  • 7.
    • C++ wasinvented by Bjarne Stroustrup in 1979, while he was working at Bell • Laboratories in Murray Hill, New Jersey. Stroustrup initially called the new language “C with Classes.” • in 1983, the name was changed to C++. • C++ extends C by adding object-oriented features.
  • 8.
    • Java wasconceived by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991 • This language was initially called “Oak” but was renamed “Java” in 1995. The Creation of Java
  • 9.
    • An applicationis a program that runs on your computer, under the operating system of that computer. • An applet is an application designed to be transmitted over the Internet and executed by a Java-compatible Web browser. • an applet is a program that can react to user input and dynamically change—not just run the same animation or sound over and over Java Applets and Applications
  • 10.
    • every timethat you download a “normal” program, you are risking a viral infection. • In addition to viruses, another type of malicious program exists that must be guarded against. • Java answers both of these concerns by providing a “firewall” between a networked application and your computer. • When you use a Java-compatible Web browser, you can safely download Security
  • 11.
    • Many typesof computers and operating systems are in use throughout the world • Many are connected to the Internet. Portability
  • 12.
    • Bytecode isa highly optimized set of instructions designed to be executed by the Java run-time system, which is called the Java Virtual Machine (JVM). • That is, in its standard form, the JVM is an interpreter for bytecode. • Translating a Java program into bytecode helps makes it much easier to run a • program in a wide variety of environments. The Bytecode
  • 13.
    • Simple • Secure •Portable • Object-oriented • Robust • Multithreaded The Java Buzzwords •Architecture-neutral •Interpreted • High performance • Distributed • Dynamic
  • 14.
    • Integers Thisgroup includes byte, short, int, and long, which are for whole valued signed numbers. • Floating-point numbers This group includes float and double, which represent numbers with fractional precision. • Characters This group includes char, which represents symbols in a character set, like letters and numbers. • Boolean This group includes boolean, which is a special type for representing true/false values. Data type
  • 15.
    • Java doesnot support unsigned, positive-only integers Integers
  • 16.
    • Floating-point numbers,also known as real numbers, are used when evaluating expressions that require fractional precision. • All transcendental math functions, such as sin(), cos( ), and sqrt( ), return double values. Floating-Point Types
  • 17.
    • Java usesUnicode to represent characters. Unicode defines a fully international character set that can represent all of the characters found in all human languages. • Thus, in Java char is a 16-bit type. The range of a char is 0 to 65,536. • There are no negative chars. Characters
  • 18.
    • Java hasa simple type, called Boolean, for logical values • It can have only one of two possible values, true or false. • This is the type returned by all relational operators Booleans
  • 19.
    • Java Isa Strongly Typed Language • Part of Java’s safety and robustness every variable has a type, every expression has a type, and every type is strictly defined. • Second, all assignments, whether explicit or via parameter passing in method calls, are checked for type compatibility. • There are no automatic coercions or conversions of conflicting types as in some languages. • The Java compiler checks all expressions and parameters to ensure that the types are compatible. • Any type mismatches are errors that must be corrected before the compiler will finish compiling the class. Java Is a Strongly Typed Language
  • 20.
    • A variableis defined by the combination of an identifier, a type, and an optional initializer. • Declaring a Variable – type identifier [ = value][, identifier [= value] ...] ; • In Java, all variables must be declared before they can be used. Variables
  • 21.
    • Java allowsvariables to be declared within any block. • A block defines a scope. Thus, each time you start a new block, you are creating a new scope. • A scope determines what objects are visible to other parts of your program. • It also determines the lifetime of those objects. The Scope and Lifetime of Variables
  • 22.
    • When youdeclare a variable within a scope, you are localizing that variable and protecting it from unauthorized access • Objects declared in the outer scope will be visible to code within the inner scope. • However, the reverse is not true. Objects declared within the inner scope will not be visible outside it. (SAMPLE WITH SWAP PROGRAM)
  • 23.
    • If thetwo types are compatible, then Java will perform the conversion automatically. • it is still possible to obtain a conversion between incompatible types. • an automatic type conversion will take place if the following two conditions are met: – The two types are compatible. – The destination type is larger than the source type. Type Conversion and Casting
  • 24.
    • It hasthis general form: – (target-type) value • if the size of the whole number • component is too large to fit into the target integer type, then that value will be reduced modulo the target type’s range. – Conversion of int to byte. – i and b 257 1
  • 25.
    • First, allbyte and short values are promoted to int, as just described. • Then, if one operand is a long, the whole expression is promoted to long. • If one operand is a float, the entire expression is promoted to float. • If any of the operands is double, the result is double. type promotion rules
  • 26.
    • An arrayis a group of like-typed variables that are referred to by a common name. • Is a homogeneous data type Arrays
  • 27.
    • The generalform of a one dimensional array declaration is – type array-var[ ]; • Here, type declares the base type of the array. • the value of array is set to null • new is a special operator that allocates memory. – array-var = new type[size]; One-Dimensional Arrays
  • 28.
    • First, youmust declare a variable of the desired array type. • Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable. • Thus, in Java all arrays are dynamically allocated.
  • 29.
    // Demonstrate aone-dimensional array. class Array { public static void main(String args[]) { int month_days[]; month_days = new int[12]; month_days[0] = 31; month_days[1] = 28; month_days[2] = 31; month_days[3] = 30; month_days[4] = 31; month_days[5] = 30; month_days[6] = 31; month_days[7] = 31; month_days[8] = 30; month_days[9] = 31; month_days[10] = 30; month_days[11] = 31; System.out.println("April has " + month_days[3] + " days."); } }
  • 30.
    • In Java,multidimensional arrays are actually arrays of arrays. • a two-dimensional array variable called twoD. – int twoD[][] = new int[4][5]; Multidimensional Arrays
  • 32.
    // Demonstrate atwo-dimensional array. class TwoDArray { public static void main(String args[]) { int twoD[][]= new int[4][5]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<5; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } }
  • 33.
    • If youneed only specify the memory for the first (leftmost) dimension. • You can allocate the remaining dimensions separately. – int twoD[][] = new int[4][]; – twoD[0] = new int[5]; – twoD[1] = new int[5]; – twoD[2] = new int[5]; – twoD[3] = new int[5];
  • 34.
    // Manually allocatediffering size second dimensions. class TwoDAgain { public static void main(String args[]) { int twoD[][] = new int[4][]; twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<i+1; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<i+1; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } } Output: 0 1 2 3 4 5 6 7 8 9
  • 35.
    – type[ ]var-name; • Eg: – int al[] = new int[3]; – int[] a2 = new int[3]; • The following declarations are also equivalent: – char twod1[][] = new char[3][4]; – char[][] twod2 = new char[3][4]; Alternative Array Declaration Syntax
  • 36.
    • Java doesnot support or allow pointers. • Java does not support pointers that can be accessed and/or modified by the programmer. • Java cannot allow pointers, because doing so would allow Java applets to breach the firewall between the Java execution environment and the host computer. Pointers in Java