Java Review

Java is the primary language used in Android development. With this, it would be proper to have a good background on the Java development concepts and formats. In this chapter, we will be having a quick overview about Java and its environment.

Java

Java is an object-oriented language, with a syntax similar to the C language. It is structured around objects and methods. A method is an action or something you do with the object.

With the object-oriented concept, this avoids the overly complicated features of C++ that includes operator overloading, pointer, templates, friend class, and many others.

How It Works…

Getting and Using Java

JDK freely downloadable from http://www.oracle.com.

All text editors support java

  1. Vi/vim, emacs, notepad, wordpad, Sublime, Notepad++, VS Code
  2. Just save to .java file

Eclipse IDE

  1. Eclipse
  2. http://www.eclipse.org
  3. Android Development Tools (ADT) is a plugin for Eclipse

Android Studio

  1. https://developer.android.com/studio

Compile and Run an Application

  1. Write java class HelloJava containing a main()  method and save in file ”HelloJava.java”
  2. The file name MUST be the same as class name
  3. Compile with: javac HelloJava.java
  4. Creates compiled .class file: HelloJava.class
  5. Run the program: java HelloJava
  6. Notice: use the class name directly, no .class!

Hello Java!

File name: HolaWorld.java

HelloJava in Eclipse

Create a New Project

  1. File -> New -> Java Project
  2. Project Name: HelloJava

Add a New Class

  1. File -> New -> Class
  2. source folder: HelloJava/src
  3. Package: excample.test
  4. Name: HelloJava
  5. check “public static void main(String[] args)

Write Your Code

  1. Add your codeSystem.out.println(“Hello Java!”);

Run You Program

  1. Run -> Run As -> Java Application

Object-Oriented

Java supports OOP

  1. Polymorphism
  2. Inheritance
  3. Encapsulation

Java programs contain nothing but definitions and instantiations of classes

  1. Everything is encapsulated in a class

Three Principles of OOP

Encapsulation: Objects hide their functions (methods) and data (instance variables)

Inheritance: Each subclass inherits all variables of its superclass

Polymorphism: Interface same despite different data types

About Class and Object

About Class

  1. Fundamental unit of Java program
  2. All java programs are classes
  3. A class is a template or blueprint for objects
  4. A class describes a set of objects that have identical characteristics (data elements) and behaviors (methods).Existing classes provided by JREUser defined classes
  5. Each class defines a set of fields (variables), methods or other classes

What is an Object?  An object is an instance of a class. An object has state, behavior and identity.

  1. Internal variable: store state
  2. Method: produce behavior
  3. Unique address in memory: identity

What does it mean to Create an Object?  An object is a chunk of memory:

  1. holds field values
  2. holds an associated object type

All objects of the same type share code

  1. they all have same object type, but can have different field values.

Class Definition and Usage

Class Usage

Person john;      //declaration
john = new Person();//create an object of Person
john.name= “John Kim”;//access its field
Person sam = new Person();
sam.name=“Sam George”;
john.printInfo();     // call method
sam.printInfo();

Class Reference

Primitive Data Types

Primitive Types

Primitive typeSizeMinimumMaximumWrapper type
boolean1-bitBoolean
char16-bitUnicode 0Unicode 216- 1Character
byte8-bit-128+127Byte
short16-bit-215+215-1Short
int32-bit-231+231-1Integer
long64-bit-263+263-1Long
float32-bitIEEE754IEEE754Float
double64-bitIEEE754IEEE754Double

Reference vs. Primitive

Java handle objects and arrays always by reference.

  1. classes and arrays are known as reference types.
  2. Class and array are composite type, don’t have standard size

Java always handle values of the primitive types directly

  1. Primitive types have standard size, can be stored in a fixed amount of memory

Because of how the primitive types and objects are handles, they behave different in two areas: copy value and compare for equality

Copy

Primitive types get copied directly by =
int x= 10; int y=x;
Objects and arrays just copy the reference, still only one copy of the object existing.

Person john = new Person();
john.name=”John”;
Person x=john;
x.name=”Sam”;
System.out.println(john.name);  // print Sam!

Scope

Scoping in a class

Access Control

Access to packages

  1. Java offers no control mechanisms for packages.
  2. If you can find and read the package you can access it

Access to classes

  1. All top level classes in package P are accessible anywhere in P
  2. All public top-level classes in P are accessible anywhere

Access to class members (in class C in package P)

  1. Public: accessible anywhere C is accessible
  2. Protected: accessible in P and to any of C’s subclasses
  3. Private: only accessible within class C
  4. Package: only accessible in P (the default)

Scope Visibility between Classes

The Static Keyword

Java methods and variables can be declared staticThese exist independent of any objectThis means that a Class’s

  1. static methods can be called even if no objects of that class have been created and
  2. static data is “shared” by all instances (i.e., one rvalue per class instead of one per instance

class StaticTest {static int i = 47;}

StaticTest st1 = new StaticTest();

StaticTest st2 = new StaticTest();

// st1.i == st2.I == 47StaticTest.i++;

// or st1.I++ or st2.I++

// st1.i == st2.I == 48

Leave a Reply

Your email address will not be published. Required fields are marked *