Java

Difference between version 47 and 48 - Previous - Next
'''[https://www.java.com%|%Java]''' is a [JVM%|%logical virtual machine] along
with an [object orientation%|%object-oriented] [programming language].


** See Also **

   [JTcl Interpreter]:   An implementation of Tcl written in Java.

   [Jacl]:   

   [TclBlend]:   

   [Java and Tcl]:   

   [Java vs Tcl]:   

   [Starting Java from Tcl]:   

   [java2tcl]:   

   [C#]:   [Microsoft%|%Microsoft's] answer to Java.

   [http://www.computerworld.com/action/article.do?command=viewArticleBasic&articleId=9127680&source=NLT_APP%|%javafxc]:   A scripting layer designed by Sun to be used by web developers as a lighter-weight entry into developing web apps.




** Documentation **

   [https://docs.oracle.com/javase/specs/jls/se13/html/index.html%|%The Java Language Specification]:   



** Description **

Java was originally created by [COMPANY: Sun Microsystems, Inc.%|%%Sun], which later made Java open source.  [Oracle]
[https://web.archive.org/web/20090423045558/http://blogs.zdnet.com/BTL/?p=16598&tag=nl.e589%|%purchased Sun] in 2009.



** Features **

   Interfaces:   For [duck typing].
   Addresses in memory are not exposed to the program:   Although a program has access to a reference to a location in memory, a program can not obtain an address in memory from a reference, and can not access memory directly by address, so the language may and does take responsibility for managing memory, including freeing memory once it is no longer in use.


** Java Basics **

This posting gives a high-level overview of basic Java language concepts and object oriented programming concepts. Feel free to correct or add to this posting what you like. [Scott Nichols]


*** Java Programs ***

There are two types of programs in Java.
   1. Java Application.<<br>><<br>>A Java application runs from your desktop or console and is usually a collection of Java class files (Java bytecodes) zipped into a Java Jar file. Note: Java JSP pages and servlets fall in this category<<br>>
   1. Java Applet.<<br>><<br>>A Java Applet runs within a web browser using some version of the Java plugin and can also be within a Jar file.<<br>>


*** Java Bytecodes ***

When java source files get compiled they are then known as Java bytecodes (*.class files) and are interpreted into machine code by the Java Virtual Machine (Java Interpreter).


*** Java Naming Conventions ***
The industry standard for Java naming convention is very simple. All classes start in uppercase and all variables and methods start in lowercase, and constants should be in all uppercase.

   1. Java Classes start in uppercase. Each individual word is capitalized in the class name. 
   1. All Java methods and variables start in lowercase. Each individual word in the name is captilized.
   1. Final variables should be written in all uppercase. This is known as a constant.

Examples: 

======none
public class SysValResponder
private final int INCREMENT = 5;
StringBuffer stringBuffer = new StringBuffer();
======

That's it for Java's naming conventions. This is the naming convention that Sun uses for their Java objects,
and you can find examples of this in Sun's Java Docs.

[DKF]: There's also the Java Bean naming conventions, which build on top of this.


*** Classes ***

A class is a collection of methods and properties. A class file also can contain inner private classes. A java source file is allowed only one public class per file. A class can either be stand alone or be extended from another class (inheritance) or can implement one or more interfaces (interfaces).

A subclass can access both protected and public members of the superclass.

If a Java class has a static '''main''' method then this is called first when the object is constructed.  A Java application can be started from only one main method. Even though several classes may have main methods within them the application itself is started from only one main.

A Java class contains a constructor method and finalize method. A Java class that is abstract does not contain a constructor. The constructor is called first, unless the class is being started from main. The Java constructor name is the same as the class name and is allowed multiple signatures. The finalize method is called when the object is destroyed or garbage collected. If the Java source code does not contain the constructor or finalize methods the Java compiler will use the default constructor and finilize method for you.

**** Example of multiple constructor signatures ****

======none
public class Circle extends Point {
    // Constructor signature One
    public Circle() {
        radius = 0;
    }
    // Constructore signature Two
    public Circle(double circleRadius) {
        radius = circleRadius;
    }
    protected void finalize() {
        System.out.println("Do any clean up work");
    }
}
======

NOTE: A class that is abstract can not be constructed with the '''new''' method call.

[DKF]:
A useful technique in more complex classes is ''constructor chaining'', which is where you define one constructor in terms of another (this must always be the first thing in the constructor).  This allows you to do simpler variations on an otherwise-complex constructor without repeating the contents of the constructor (which can be a real problem for maintenance.)

**** Example of multiple constructors showing constructor chaining ****

======none
public class Circle extends Point {
    // Constructor signature One
    public Circle() {
        this(0.0); // chain to other constructor
    }
    // Constructor signature Two
    public Circle(double circleRadius) {
        radius = circleRadius;
    }
    protected void finalize() {
        System.out.println("Do any clean up work");
    }
}
======


*** Inheritance ***

A class can inherit methods and properties from another class.  This is known as inheritance. The class that your subclass is inheriting from is known as the ''superclass''. The class you write is known as the ''subclass''. Java only supports one level of inheritance: meaning that the subclass can only be extended from one superclass. But, Java supports multiple interfaces to get around this.


*** Example on Abstract inheritance ****

(you will see this a lot in Java):

======none
// File PizzaStore.java
public abstract class PizzaStore {
   public abstract void run();
}

// File PizzaHut.java
// In this example, the class PizzaHut is 
// being extended from PizzaStore and
// the method run, is overwriting the method
// run, in the superclass. This is known as
// Polymorphism.
public class PizzaHut extends PizzaStore {
    // Implemented method run
    public void run() {
        System.out.println("Pizza Hut");
    }
}

// File Nancys.java
public class Nancys extends PizzaStore {
    // Overridden method run
    public void run() {
        System.out.println("Nancys");
    }
}

// File MyStores.java
public class MyStores {
    public static void main(String[] args) {
        // The one and only Inheritance PizzaStore variable
        PizzaStore myStore;
  
        // Set myStore to PizzaHut
        myStore = new PizzaHut();
        myStore.run();
  
        // Set myStore to Nancys
        myStore = new Nancys();
        myStore.run();
    }
}
======


*** Interfaces ***

A Java Interface is a collection of methods only. A interface does not contain any properties. Interfaces are commonly used as a way of enforcing policy. A interface can not be constructed. It can only be implemented or declared by the implementing class.

Important Note: A subclass can implement multiple interfaces, but can only  inherit from one superclass.

Abstract Interface's methods only contain a signature of the method. The body of the method must be implemented in the implementing class.

Note that all methods in an Interface are implicitly abstract. Thus the abstract keyword in the method declaration is optional.

**** Abstract Interfaces Example ****

======none
// File ipizzaStore
public interface IpizzaStore {
    // Keyword abstract is optional here
    public abstract void run();
}

// File IPizzaHut.java
public class IPizzaHut implements IpizzaStore {
    public void run() {
        System.out.println("Pizza Hut");
    }
}

// File INancys.java
public class INancys implements IpizzaStore {
    public void run() {
        System.out.println("Nancys");
    }
}

// File IMystores.java
public class IMyStores {
    public static void main(String[] args) {
        // The one and only Interface IPizzaStore variable
        IpizzaStore myStore;
  
        // Set myStore to PizzaHut
        myStore = new IPizzaHut();
        myStore.run();
  
        // Set myStore to Nancys
        myStore = new INancys();
        myStore.run();
    }
}
======


*** Method Overloading ***

Method overloading is used in a Java classes when methods have the same name (within the same class), but have different parameters. This is known as the Java signature.

Example:


======none
// In this example the SampleMath.java contains two overloaded 
// square methods that accept both integer 
// and double parameters
public class SampleMath {
    public double square(double doubleValue) {
        returnd doubleValue * doubleValue;
    }
    public int square(int intValue) {
        returnd intValue * intValue;
    }  
}
======


*** Method Overriding ***

Method overriding is when a inherited class over writes a method from the superclass. This is a form of polymorphism.

It has some rules 

   1. The overriden method name and parameter list should be same and same type
   1. Access spcifer in super class method and drived class method should be same,
   1. The exception on the method declaration of Throws class , must be sub set or not define

Please see my PizzaHut example above with the run method.


*** The final Keyword ***

A variable that is declared with the keyword final value can not be updated. This is handy for setting up constants. Constants are usually written in all uppercase. The final keyword can also be used with a method to keep a extended subclass from overriding the method.

Example:

======none
private final int INCREMENT = 5;
======

*** Static Variables and Methods ***

A ''static'' class variable or method is ''shared by all objects of a class''. If the static variable is updated by a shared object then all objects of the same type (class) will see the change. 
Static variables are often used as counters. This can be handy in a JSP page to find out how many users are currently connected to your Servlet.

*** this and super keywords ***
The ''this'' keyword followed by a dot is used to access members variables and methods of the current object.

Example:

======none
public SimpleTime(int hour, int minute, int second) {
    this.hour = hour;
    this.minute = minute;
    this.second = second;
}
======

The ''super'' keyword followed by a dot is used to access the original version of the method from the subclass. The super keyword can be handy if you want to access the original method in the superclass that has not been overwritten in your subclass


*** Pass-By-Value and Pass-By-Reference ***

When calling a method and passing parameters, primative data types 
(`char`, `byte`, `short`, `int`, `long`, `float`, `double`, or `boolean`) are transferred to the method by ''pass-by-value''. 
And objects, collections, vectors, and arrays are transferred to the new method as ''pass-by-reference''. 

When the contents of an object are changed when doing pass-by-reference the object's contents in the calling class are changed too.

Note:
     In java is supporting " CALL-BY-VALUE"



** Java Programming Tips **

The heart of Java programming is inheritance and interfaces. Both of these concepts allow for reusability and scalability of Java code. Once you understand both of these concepts your Java programming will happen much easier. If you understand the concepts the syntax will come on its own.


*** JSP and Java Servlet Hint ***

If you will be doing a lot of string concatenations, use the class java.lang.StringBuffer instead of java.lang.String. StringBuffer is ''much'' faster at appending strings together. 

<<categories>> Language | Java