Java is a logical virtual machine along with an object-oriented programming language.
Java was originally created by %Sun, which later made Java open source. Oracle purchased Sun in 2009.
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
There are two types of programs in Java.
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).
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.
Examples:
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.
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.
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.)
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"); } }
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.
(you will see this a lot in Java):
// 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(); } }
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.
// 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 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:
// 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 is when a inherited class over writes a method from the superclass. This is a form of polymorphism.
It has some rules
Please see my PizzaHut example above with the run method.
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:
private final int INCREMENT = 5;
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.
The this keyword followed by a dot is used to access members variables and methods of the current object.
Example:
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
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"
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.
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.