Overview of Java Programming Language

Intro to Java
L. Grewe
Java history
James Gosling and others at Sun,
1990 - 95
Internet application
simple language for writing programs
that can be transmitted over network
Some Goals
Portability
Internet-wide distribution:  PC, Unix, Mac
Reliability
Avoid program crashes and error messages
Safety
Programmer may be malicious
Simplicity and familiarity
Appeal to average programmer; less complex
than C++
Efficiency
Important but secondary
Characteristics
Simplicity
Everything an object
All objects on heap, accessed through pointers
no functions
no multiple inheritance,
no operator overloading
Portability
Bytecode interpreter on many platforms
Reliability and Safety
Run-time type and bounds checks
Garbage collection
Java System
The Java programming language
Compiler and run-time system
Programmer compiles code
Compiled code transmitted on network
Receiver executes on interpreter (JVM)
Safety checks made before/during execution
Library, including graphics, security, etc.
Large library
Interoperability
Provision for “native” methods
Java Release History
1995 (1.0) – First public release
1997 (1.1) – Inner classes
2001 (1.4) – Assertions
Verify programmers understanding of code
2004 (1.5) – Tiger
Generics, foreach, Autoboxing/Unboxing,
Typesafe Enums, Varargs, Static Import,
Annotations, concurrency utility library
2006 (1.6) – SE 6
Enhancements since  JDK 5 (=
Java 1.5)
Generics
Polymorphism and compile-time type safety (JSR 14)
Enhanced for Loop
For iterating over collections and arrays (JSR 201)
Autoboxing/Unboxing
Automatic conversion between primitive, wrapper types
(JSR 201)
Typesafe Enums
Enumerated types with arbitrary methods and fields (JSR
201)
Varargs
Puts argument lists into an array; variable-length
argument lists
Static Import
Avoid qualifying static members with class names (JSR
201)
Annotations
 (Metadata)
Enables tools to generate code from annotations (JSR 175)
Concurrency utility library
Language Terminology
Class, object
Field – data member
Method - member function
Static members - class fields and
methods
this - self
Package - set of classes in shared
namespace
Native method - method written in
another language, often C
Java Classes and Objects
Syntax similar to C++
Object
has fields and methods
is allocated on heap, not run-time stack
accessible through reference (only ptr
assignment)
garbage collected
Dynamic lookup
Similar in behavior to other languages
Static typing => more efficient than Smalltalk
Dynamic linking, interfaces => slower than
C++
Template for Class Definition
Point Class
class Point {
    private int x;
    protected void setX (int y)  {x = y;}
    public int  getX()     {return x;}
    Point(int xval) {x = xval;}       //
constructor
};
Object initialization
Java guarantees constructor call for
each object
Memory allocated
Constructor called to initialize memory
Some interesting issues related to inheritance
Garbage Collection and Finalize
Objects are garbage collected
No explicit 
free
Avoids dangling pointers and resulting type errors
Problem
What if object has opened file or?
Solution
finalize
  method, called by the garbage collector
Before space is reclaimed, or when virtual
machine exits
Encapsulation and packages
Every field,
method belongs
to a class
Every class is part
of some package
Can be unnamed
default package
File declares which
package code
belongs to
Access
Four access distinctions
public, private, protected, package
Method can refer to
private members of class it belongs to
non-private members of all classes in same package
protected members of superclasses (in diff package)
public members of classes in visible packages
Visibility determined by files system, etc. (outside
language)
Qualified names  
(or use import)
java.lang.String.substring()
package
class
method
Inheritance
Similar to Smalltalk, C++
Subclass inherits from superclass
Single inheritance only (but Java
has interfaces)
Example subclass
class ColorPoint 
class ColorPoint 
extends
extends
 Point {
 Point {
   // Additional fields and methods
   // Additional fields and methods
    private Color c;
    private Color c;
    protected void setC (Color d)  {c = d;}
    protected void setC (Color d)  {c = d;}
    public Color  getC()     {return c;}
    public Color  getC()     {return c;}
   // Define constructor
   // Define constructor
    ColorPoint(int xval, Color cval) {
    ColorPoint(int xval, Color cval) {
         super(xval);    // call Point
         super(xval);    // call Point
constructor
constructor
         c = cval;  }     // initialize
         c = cval;  }     // initialize
ColorPoint field
ColorPoint field
 };
 };
Class 
Object
Every class extends another class
Every class extends another class
Superclass is 
Superclass is 
Object
Object
 
 
if no other class
if no other class
named
named
Methods of class
Methods of class
 Object
 Object
GetClass – return the Class object representing class of
GetClass – return the Class object representing class of
the object
the object
ToString – returns string representation of object
ToString – returns string representation of object
equals – default object equality (not ptr equality)
equals – default object equality (not ptr equality)
hashCode
hashCode
Clone – makes a duplicate of an object
Clone – makes a duplicate of an object
wait, notify, notifyAll – used with concurrency
wait, notify, notifyAll – used with concurrency
finalize
finalize
Constructors and Super
Java guarantees constructor call for each
object
Different conventions for finalize and super
 Compiler does not force call to super finalize
Final classes and methods
Restrict inheritance
Final classes and methods cannot be redefined
Example
   java.lang.String
Reasons for this feature
Important for security
Programmer controls  behavior of all subclasses
Critical because subclasses produce subtypes
Compare to C++ virtual/non-virtual
Method is “virtual” until it becomes final
Call-by-Value Parameter
Passing
When a method is called,
When a method is called,
value of argument is passed to the matching parameter,
value of argument is passed to the matching parameter,
separate memory space is allocated to store this value.
separate memory space is allocated to store this value.
This way of passing the value of
This way of passing the value of
arguments is called a 
arguments is called a 
pass-by-value
pass-by-value
Since separate memory space is allocated
Since separate memory space is allocated
for each parameter during the execution
for each parameter during the execution
of the method,
of the method,
the parameter is local to the method,
the parameter is local to the method,
changes made to the parameter will not affect the value
changes made to the parameter will not affect the value
of the corresponding argument.
of the corresponding argument.
Java Types
Two general kinds of types
Primitive types – 
not
 objects
Integers, Booleans, etc
Reference types
Classes, interfaces, arrays
No syntax distinguishing  Object * from Object
Classification of Java types
Reference Types
Primitive Types
int
Shape
Object[ ]
Object
Shape[ ]
boolean
Throwable
Square
Square[ ]
Circle
Circle[ ]
long
float
byte
Exception
types
user-defined
arrays
Arrays
Automatically defined
Automatically defined
Can have for primitive types and objects.
Can have for primitive types and objects.
Multi-dimensional arrays T[ ] [ ]
Multi-dimensional arrays T[ ] [ ]
Treated as reference type
Treated as reference type
An array variable is a pointer to an array, can be
An array variable is a pointer to an array, can be
null
null
Example: 
Example: 
Circle[] x = new Circle[array_size]
Circle[] x = new Circle[array_size]
Every array type is a subtype of
Every array type is a subtype of
Object[ ],  Object
Object[ ],  Object
Length of array is not part of its static type
Length of array is not part of its static type
Interface example
interface Shape {
interface Shape {
 
 
public float center();
public float center();
   public void rotate(float degrees);
   public void rotate(float degrees);
}
}
interface Drawable {
interface Drawable {
 
 
public void setColor(Color c);
public void setColor(Color c);
   public void draw();
   public void draw();
}
}
class Circle
class Circle
 implements
 implements
 Shape, Drawable {
 Shape, Drawable {
 
 
// does not inherit any implementation
// does not inherit any implementation
   // but must define Shape, Drawable methods
   // but must define Shape, Drawable methods
}
}
Interfaces
Flexibility
Black box design….specify input/output
but, not internals.
Java Exceptions
Similar basic functionality to C++
Constructs to 
throw
 and 
catch
 exceptions
Dynamic scoping of handler
Some differences
An exception is an object from an exception class
Subtyping between exception classes
Use subtyping to match type of exception or pass
it on …
Type of method includes exceptions it can throw
Exception Classes
If a method may throw a checked exception,
then this must be in the type of the method
Throwable
Exception
Runtime
Exception
Error
User-defined
exception classes
Unchecked exceptions
checked 
exceptions
Try/finally blocks
Exceptions are caught in try blocks
try {
  
statements
}
  
catch (ex-type1 identifier1) {
   
statements
} catch (ex-type2 identifier2) {
   
statements
}
 
finally {
   
statements
}
Keyword “this"
Object can use to refer to itself.
Can use to refer to its variables…see
example.
class 
Person
 {
 
int  age
;
 
 
 
public void 
setAge(
int 
val) 
{
 
 
   this
.age = val;
 
}
 
. . .
}
Overloaded Methods
Methods can share the same name
Methods can share the same name
as long as
as long as
different number of parameters (
different number of parameters (
Rule 1)
Rule 1)
or
or
their parameters are of different types
their parameters are of different types
when the number of parameters is the
when the number of parameters is the
same (
same (
Rule 2
Rule 2
)
)
public void 
myMethod
(int 
x, 
int 
y
) { 
... 
}
public void 
myMethod
(int 
x
) { 
... 
}
public void 
myMethod
(double 
x
) { 
... 
}
public void 
myMethod
(int 
x
) { 
... 
}
Rule 1
Rule 2
Simple Console Application
Lets create Hello World in Java
Lets create Hello World in Java
Single class
Single class
Contains VERY special method with
Contains VERY special method with
signature:
signature:
public static void main(String args) {
public static void main(String args) {
        
        
//your code here
//your code here
}
}
Simple Console Application
Here we see it in Eclipse
Slide Note
Embed
Share

Java is a versatile and powerful programming language created by James Gosling and others at Sun Microsystems in the 1990s. It aims for portability, reliability, safety, simplicity, and efficiency. With a strong focus on object-oriented programming, Java has evolved over the years with a rich history of releases and enhancements, making it a popular choice for a wide range of applications. The language features characteristics such as simplicity, portability, reliability, safety, and a comprehensive system that includes a compiler, run-time environment, and a vast library. Java's release history includes significant milestones like the introduction of inner classes, generics, annotations, and more. Enhancements since JDK 5 have further improved Java's capabilities with features like generics, enhanced for loop, autoboxing, typesafe enums, varargs, static import, and concurrency utility library.

  • Java programming
  • History
  • Characteristics
  • Release history
  • Enhancements

Uploaded on Sep 28, 2024 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

E N D

Presentation Transcript


  1. Intro to Java L. Grewe

  2. Java history James Gosling and others at Sun, 1990 - 95 Internet application simple language for writing programs that can be transmitted over network

  3. Some Goals Portability Internet-wide distribution: PC, Unix, Mac Reliability Avoid program crashes and error messages Safety Programmer may be malicious Simplicity and familiarity Appeal to average programmer; less complex than C++ Efficiency Important but secondary

  4. Characteristics Simplicity Everything an object All objects on heap, accessed through pointers no functions no multiple inheritance, no operator overloading Portability Bytecode interpreter on many platforms Reliability and Safety Run-time type and bounds checks Garbage collection

  5. Java System The Java programming language Compiler and run-time system Programmer compiles code Compiled code transmitted on network Receiver executes on interpreter (JVM) Safety checks made before/during execution Library, including graphics, security, etc. Large library Interoperability Provision for native methods

  6. Java Release History 1995 (1.0) First public release 1997 (1.1) Inner classes 2001 (1.4) Assertions Verify programmers understanding of code 2004 (1.5) Tiger Generics, foreach, Autoboxing/Unboxing, Typesafe Enums, Varargs, Static Import, Annotations, concurrency utility library 2006 (1.6) SE 6

  7. Enhancements since JDK 5 (= Java 1.5) Generics Polymorphism and compile-time type safety (JSR 14) Enhanced for Loop For iterating over collections and arrays (JSR 201) Autoboxing/Unboxing Automatic conversion between primitive, wrapper types (JSR 201) Typesafe Enums Enumerated types with arbitrary methods and fields (JSR 201) Varargs Puts argument lists into an array; variable-length argument lists Static Import Avoid qualifying static members with class names (JSR 201) Annotations (Metadata) Enables tools to generate code from annotations (JSR 175) Concurrency utility library

  8. Language Terminology Class, object Field data member Method - member function Static members - class fields and methods this - self Package - set of classes in shared namespace Native method - method written in another language, often C

  9. Java Classes and Objects Syntax similar to C++ Object has fields and methods is allocated on heap, not run-time stack accessible through reference (only ptr assignment) garbage collected Dynamic lookup Similar in behavior to other languages Static typing => more efficient than Smalltalk Dynamic linking, interfaces => slower than C++

  10. Template for Class Definition Import Statements Class Comment class { Class Name Fields/Variables Methods (incl. Constructor) }

  11. Point Class class Point { private int x; protected void setX (int y) {x = y;} public int getX() {return x;} Point(int xval) {x = xval;} // constructor };

  12. Object initialization Java guarantees constructor call for each object Memory allocated Constructor called to initialize memory Some interesting issues related to inheritance

  13. Garbage Collection and Finalize Objects are garbage collected No explicit free Avoids dangling pointers and resulting type errors Problem What if object has opened file or? Solution finalize method, called by the garbage collector Before space is reclaimed, or when virtual machine exits

  14. Encapsulation and packages Every field, method belongs to a class Every class is part of some package Can be unnamed default package File declares which package code belongs to package class field method package class field method

  15. Access Four access distinctions public, private, protected, package Method can refer to private members of class it belongs to non-private members of all classes in same package protected members of superclasses (in diff package) public members of classes in visible packages Visibility determined by files system, etc. (outside language) Qualified names (or use import) java.lang.String.substring() method class package

  16. Inheritance Similar to Smalltalk, C++ Subclass inherits from superclass Single inheritance only (but Java has interfaces)

  17. Example subclass class ColorPoint extends Point { // Additional fields and methods private Color c; protected void setC (Color d) {c = d;} public Color getC() {return c;} // Define constructor ColorPoint(int xval, Color cval) { super(xval); // call Point constructor c = cval; } // initialize ColorPoint field };

  18. Class Object Every class extends another class Superclass is Object if no other class named Methods of class Object GetClass return the Class object representing class of the object ToString returns string representation of object equals default object equality (not ptr equality) hashCode Clone makes a duplicate of an object wait, notify, notifyAll used with concurrency finalize

  19. Constructors and Super Java guarantees constructor call for each object Different conventions for finalize and super Compiler does not force call to super finalize

  20. Final classes and methods Restrict inheritance Final classes and methods cannot be redefined Example java.lang.String Reasons for this feature Important for security Programmer controls behavior of all subclasses Critical because subclasses produce subtypes Compare to C++ virtual/non-virtual Method is virtual until it becomes final

  21. Call-by-Value Parameter Passing When a method is called, value of argument is passed to the matching parameter, separate memory space is allocated to store this value. This way of passing the value of arguments is called a pass-by-value Since separate memory space is allocated for each parameter during the execution of the method, the parameter is local to the method, changes made to the parameter will not affect the value of the corresponding argument.

  22. Java Types Two general kinds of types Primitive types not objects Integers, Booleans, etc Reference types Classes, interfaces, arrays No syntax distinguishing Object * from Object

  23. Classification of Java types Reference Types Object Object[ ] Throwable Shape Shape[ ] Exception types Circle Square Circle[ ] Square[ ] user-defined arrays Primitive Types boolean int byte float long

  24. Arrays Automatically defined Can have for primitive types and objects. Multi-dimensional arrays T[ ] [ ] Treated as reference type An array variable is a pointer to an array, can be null Example: Circle[] x = new Circle[array_size] Every array type is a subtype of Object[ ], Object Length of array is not part of its static type

  25. Interface example interface Shape { public float center(); public void rotate(float degrees); } interface Drawable { public void setColor(Color c); public void draw(); } class Circle implements Shape, Drawable { // does not inherit any implementation // but must define Shape, Drawable methods }

  26. Interfaces Flexibility Black box design .specify input/output but, not internals.

  27. Java Exceptions Similar basic functionality to C++ Constructs to throw and catch exceptions Dynamic scoping of handler Some differences An exception is an object from an exception class Subtyping between exception classes Use subtyping to match type of exception or pass it on Type of method includes exceptions it can throw

  28. Exception Classes Throwable Runtime Exception Exception Error checked exceptions User-defined exception classes Unchecked exceptions If a method may throw a checked exception, then this must be in the type of the method

  29. Try/finally blocks Exceptions are caught in try blocks try { statements } catch (ex-type1 identifier1) { statements } catch (ex-type2 identifier2) { statements } finally { statements }

  30. Keyword this" Object can use to refer to itself. Can use to refer to its variables see example. class Person { } int age; public void setAge(int val) { this.age = val; } . . .

  31. Overloaded Methods Methods can share the same name as long as different number of parameters (Rule 1) or their parameters are of different types when the number of parameters is the same (Rule 2) public void myMethod(int x, int y) { ... } Rule 1 public void myMethod(int x) { ... } public void myMethod(double x) { ... } Rule 2 public void myMethod(int x) { ... }

  32. Simple Console Application Lets create Hello World in Java Single class Contains VERY special method with signature: public static void main(String args) { //your code here }

  33. Simple Console Application Here we see it in Eclipse

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#