Docsity

Java Language Essentials: Object-Oriented Programming, Variables, Types, and Flow Control, Slides of Java Programming

Toggle sidebar
bg1
Elements of Java
•This chapter is a catalog of the Java language.
It contains a lot of information in a highly
compressed format. Some students may wish
to use another supplementary Java textbook
that has a lot of exercises to practice different
features of the language instead of having so
many at once. There are lots of such texts in
bookstores.
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22

Partial preview of the text

Download Java Language Essentials: Object-Oriented Programming, Variables, Types, and Flow Control and more Slides Java Programming in PDF only on Docsity!

Elements of Java

  • This chapter is a catalog of the Java language. It contains a lot of information in a highly compressed format. Some students may wish to use another supplementary Java textbook that has a lot of exercises to practice different features of the language instead of having so many at once. There are lots of such texts in bookstores.

Java Uniqueness

  • Purely object-oriented—no compromises!
  • Primitive types are not objects
  • No multiple inheritance (simulate with interfaces)
  • No GOTO statement
  • 16 bit international characters only (Unicode)
  • Exception handling
  • Automatic memory management (garbage collection)
  • Multithreaded programming.

Variables and Types

  • A variable is a location in memory where values can be stored and referenced.
  • Variables are associated with types, which have particular data sizes and a restricted set of valid operations that can be performed on them.
  • Variable declarations assign an identifier, a type, and may declare an initial value. Many types have a default value. For example, an uninitialized integer defaults to 0.

Variable (a memory location)

address

a value (a default value)

a name

a type

The address or value may be passed as a parameter

Point example (text, p. 104)

public class Point { public double x, y; public Point() { //no-arg constructor x = 0.0; y = 0.0; } public Point(double init_x, double init_y) { x = init_x; y = init_y; …^ } }

  • Here, the no-arg constructor supplies default values for the point location.

Singleton Classes

  • Classes that are not supposed to have more than one instance at a time, such as a top- level window or a system timer, can be limited as a singleton class, an instance of the Singleton design pattern that will be discussed in a future lecture.
  • The following slide shows a singleton class.

Reference Types

  • References to classes, interfaces and arrays are implemented in Java as 32 bit pointers and are called reference types.
  • While C and C++ pointers can be cast to other types, that is forbidden in Java. No direct manipulation of reference types is permitted. This reduces errors and increases security.

Variable Scope

  • Local variables are only valid within the block in which they are declared. Blocks in Java open with { and close with } and can have other blocks embedded in them. Local variables are not automatically initialized. Variable declarations must be terminated with a ;

Class Declarations

  • Class declarations define classes, which can be instantiated as objects and are assigned to a reference type.
  • A class declaration includes a class name and fields, methods, and nested class declarations. The fields are known as the variables or attributes of a class.

Class and Method Modifiers

  • < none > –Field is accessible by all others within a package
  • public –Field is accessible by any class
  • private –Field is accessible only by itself
  • static –Field is shared by all instances of a class
  • abstract –Class contains only abstract methods, method defers implementation to subclasses
  • final –Field may not be extended with subclasses

A field may be a class or method.

main Method

  • Every program must have a main method, declared as public static void main(String [] args) { … }
  • The array of String arguments allows initialization values to be passed to the class at start up time. Only one class in the program requires a main method.

Print Command args (NetBeans)

More things to understand

  • • Multidimensional arrays p.
  • • Expressions for object creation p.
  • • Local variables and scope p.
  • • The return statement p.
  • • Break and continue p.
  • • Class and method modifiers pp. 101-
  • • Accessibility p.
  • • Constructors p. More things to understand
  • • Accessors p.
  • • Parameter Passing pp. 107-
  • • Class fields pp. 110-
  • • Singletons p.
  • • This pp.114-
  • • Interfaces pp. 117-