Anda di halaman 1dari 1

Java Cheat Sheet Conditionals public int getIntegerNumber() { ...

}
== Equals
Hello World <= Less than or equal Method with two parameters:
public class HelloWorld { < Less than
public static void main (String [] args) >= Greater than or equal public void setSize(int height, int width) { ... }
{ > Greater than
System.out.println("Hello World"); != Not equal
} ! Not Static
} && And Static methods are called without creating an object.
|| Or Static methods may not access non-static variables or methods.
Java Basics Static variables are shared across all instances.
java edu.simpson.ClassName Run main in ClassName Strings
javac *.java Compile .java to .class s1.equals(s2) Compare two strings Inheritance
A class goes in a .java file. s1.equalsIgnoreCase(s2) Compare, ignoring case
A subclass extends a superclass.
Methods and attributes go in a class. s1.length() Return length of string
A child extends a parent.
Statements go in methods. String[]a=s1.split(" "); Split string separated by spaces
A child inherits all the parent’s attributes and methods.
A block is contained in { } Loops Methods can be overridden with new functionality, attributes
Comments while(i<10) { ... } Pretest can not.
// Comment to end of line do { ... } while (i<10); Post-test All objects have the Object class as their top parent.
/* x */ Comment everything between for(int i=0;i<10;i++) { ... } For loop To create a child: class Child extends Parent {
/** x */ Javadoc comment To call a parent constructor (must be first line in the
Branches constructor): super( ... );
Primitive Variables if(i<10){
type bytes range // do something Interface
byte 8 -128..127 } else if(i>10) {
An interface is a pure abstract class that defines a protocol
short 16 -32,768..32,767 // do something else
To declare an interface:
int 32 -2,147,483,648..2,147,483,647 } else {
long 64 −263 to 263 − 1 // do if nothing else matched
float 32 1.4e-45..3.4e+38 } public interface MyInterface {
double 64 4.9e-324..1.7e+308 void myFunction(); // No method body
char 2 Unicode letter, 0..65,535 Classes }
boolean true..false Classes contain a blueprint of all the attributes, and methods
Variables should begin with a lower case letter. By default, for an object. Input
numbers are int or double. Append F for float, L for long,
public class Person { Get input from a user:
and D for double. Use single quotes for a char.
// Attribute
Declaration:
private int age; Scanner scan = new Scanner(System.in);
datatype variablename;
private String name; int a = scan.nextInt(); // Get an integer
Objects // Constructor that sets the name String b = scan.next(); // Get text
Objects are created from classes. public Person(String name) {
this.name=name; Get input from a file:
Person myPerson; }
myPerson = new Person(); // Method that returns an age
FileInputStream in = new FileInputStream("file.txt");
Reference Variables public int getAge() {
Scanner scan = new Scanner(in);
return age;
These contain a memory address where an object exists.
}
Access object variables with dot operator:
// Method that sets an age Arrays
myPerson.name="Fred";
public void setAge(int age) { Create an integer array: int [] a=new int[50];
Access object methods with dot oeprator:
this.age = age; Assign first value: a[0]=5;
int x = myPerson.getAge()
} Assign last value: a[49]=5;
Expressions }
= Assignment (Don’t confuse with ==) Libraries and packages
Methods
* Multiply Import a package: import java.util.Date;
\ Divide Simple method:
% Modulus (remainder) public void doSomething() { ... }
x++ Return x, then increment $Revision: 1.0 $, $Date: 2007/11/27 $.
Method that returns a value:
++x Increment, then return x
x+=2 Add 2 to x, store in x
x*=2 Multiply x by 2, store in x
func(x) Run code in func, return result

Anda mungkin juga menyukai