Java Syntax Summary

Share Embed Donate

Short Description



Java Syntax Summary Java Basics Java source code is defined in a .java file. The name of the class at the beginning of the file must be the same as the name of the file. (For example, Variables and methods are defined in a class. Statements are defined in methods. A block is contained inside brace {curly} brackets. When a class is compiled, a .class file is created. The .class file contains the bytecode that corresponds to the Java source file.

Conditional Operator (expression) ? true-value: false-value; max = (value1>value2) ? value1 : value2; public class HelloWorld { public static void main (String [] args) { System.out.println("Hello World"); } }

for (i=0, j=5; i10 || j==3) { // do something else } else { // do if nothing else matched }

Catch I/O Errors try { input/output statements; } catch (IOException ioe) { System.out.println(ioe.getMessage()); }


I/O Imports import; import; import; import; import;

Objects Declaration of objects: Person myPerson1; Person myPerson2; Instantiation of objects using the new operator: myPerson1 = new Person("Fred"); myPerson2 = new Person("Sally");

Useful Functions System.arraycopy(from,f-position,to,to-position,length); Arrays.toString(array) // returns a String that contains // all of the elements in array Math.random() // returns a random value ∈ [0,1)

Invoking Instance Methods object.method([parms]); myPerson1.setAge(30); myPerson2.setAge(20); age = myPerson1.getAge();

Data Formatting Format one or more values and return the result as a String. Can be used with: fileName.format and string.format .format(formatString, value1, value2, …) %[flags][width][.precision]type "%5d" integer "%6.2f" floating point "%5s" string "%n" newline flags: "%-5d" left justify flags: "%05d" fill unused high-order positions with 0’s

Static Normally, all variables and methods in the “main” class are defined as static. Static (class) methods are called without creating an object. Static (class) methods may not access non-static (instance) variables or methods. Static (class) variables are shared among all instances of the class.

Classes A class defines the variables and methods for a new data type. (The class is a blueprint for the data type.) The name of a class should begin with an upper-case letter. If a class is defined in the same file as the “main” class, omit the keyword public from the definition of the class. Objects are declared to be of type class where the class may be a system-defined class or a programmer-defined class.

Inheritance A subclass extends a superclass. A child extends a parent. A child inherits all the parent’s variables and methods. Methods can be overridden with new functionality, variables can not be overridden. All objects have the Object class as their top parent. To create a child: class Child extends Parent { ... } To call a parent’s constructor (must be first line in the constructor): super( ... );

[public] class Person { // Instance variables private String name; private int age;

Access/Visibility Modifiers public // can be accessed by any class private // can be accessed only within the current class protected // can be accessed only by the current class // and its subclasses

// Constructor that saves the "name" parameter // in the instance variable "name" public Person(String name) {; }

ArrayLists An ArrayList is a class that maintains a collection of objects and that provides more features than the basic array. ArrayList myList; myList = new ArrayList(); myList.add("We"); string = (String) myList.get(0); myList.add("need"); myList.add("some"); myList.remove(0); myList.add(0, "I"); myList.add("help"); myList.set(2, "more"); System.out.println(myList); [I, need, more, help] Adding a data type to an ArrayList: ArrayList myList;

// Method that returns the value of age public int getAge() { return age; } // Method that sets the value of age public void setAge(int age) { this.age = age; } public String toString() { return name +" " +age; }

This document was adapted from: Last modified: December 10, 2012.


David Scuse


View more...


Copyright ©2017 KUPDF Inc.