
Java source code is stored in a file with the extension .java. Compiling the code creates one or more .class files, which contain processor-independent byte codes. The Java Virtual Machine (JVM) translates the byte code into machine-level instructions for the processor on which the Java application is running.
/* Comments
Comments
*/
public class ClassName {
public static void main(String [] args) {
System.out.println("Hello World!");
}
}
ClassName ObjectReference; objectReference = new ClassName(argument list);
ClassName ObjectReference = new ClassName(argument list);
Date birthday = new Date(12, 6, 1985);
public class SimpleLocation{
public double latitude; /* member variables */
public double longitude;
public SimpleLocation(double lat, double lon){ /* Constructor */
this.latitude = lat;
this.longitude = lon;
}
public double distance(SimpleLocation other){
return Math.sqrt(Math.pow((this.latitude - other.latitude),2)
+Math.pow((this.longitude - other.longitude),2));
}
}
public class LocationTester{
public static void main(String[] args){
SimpleLocation place1 = new SimpleLocation(1, 2);
SimpleLocation place2 = new SimpleLocation(4, 8);
System.out.println(place1.distance(place2));
}
}
public class SimpleLocation {
private double latitude;
private double longitude;
public double getLatitude() {
return this.latitude;
}
public void setLatitude(double lat) {
if (lat>100) {
System.out.println("Illegal value");
}
else {
this.latitude = lat;
}
}
}
import java.text.DecimalFormat; import java.text.*;
imports the class DecimalFormat imports all classes in the package
String s1 = new String("Hello");
String s2 = "World!";
System.out.println(s1 + " " + s2);
int lens1 = s1.length();
System.out.println(lens1 + s2.length());
int indexw = s2.indexOf('W');
System.out.println(s1.substring(0,2));
System.out.println(s1.charAt(4));
Hello World! 11 (0) He o
import javax.swing.JOptionPane;
public class DialogBoxDemo {
public static void main(String[] args) {
String name = JOptionPane.showInputDialog(null, "Please enter your name");
JOptionPane.showMessageDialog(null, "Hello, " + name);
String yearStr = JOptionPane.showInputDialog(null, "At which year you were born?");
int yearInt = Integer.parseInt(yearStr);
JOptionPane.showMessageDialog(null, "You are " + (2016 - yearInt) + " years old");
}
}
import java.util.Scanner;
public class JavaConsoleDemo {
public static void main( String [] args ) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter your first name: ");
String firstName = scan.next();
System.out.println("Hello " + firstName);
System.out.print("At which year you were born? ");
int birthYear = scan.nextInt();
System.out.println("You are " + (2016 - birthYear) + " years old");
}
}
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class EchoFileData {
public static void main( String [] args ) throws IOException {
int number;
File inputFile = new File("input.txt");
Scanner scan = new Scanner(inputFile);
while (scan.hasNext()) {
number = scan.nextInt();
System.out.println(number);
}
System.out.println("End of File Detected");
}
}
import java.swing.JApplet;
import java.awt.Graphics;
public class ShellApplet extends JApplet {
public void init() {
}
public void paint(Graphics g) {
super.paint(g);
}
}
import java.swing.JApplet;
import java.awt.Graphics;
public class AppletDemo extends JApplet {
public void paint(Graphics g) {
super.paint(g);
g.drawString("Hello World!", 140, 100);
g.drawLine(100,150,100,250); // a vertical line
g.setColor(Color.RED); // From now on the color of drawigs will be red
}
}<APPLET CODE="ClassName.class" CODEBASE="Path to the directory of class file" WIDTH=w HEIGHT=h></APPLET>
if (condition) {
block }
if (condition)
statement;
Date d1 = new Date(12, 6, 1985);
Date d2 = new Date(12, 6, 1985);
Date d3 = d1;
if (d1 == d2)
System.out.println('yes');
if (d1 == d3)
System.out.println('yes');
if (d1.equals(d2))
System.out.println('yes');
if (d1.equals(d3))
System.out.println('yes');
yes yes yes
variable = (condition ? expression1 : expression2);
if (condition)
variable = expression1;
else
variable = expression2;int door;
if (inputNum == 2)
door = inputNum;
else door =1; is equivalent to
int door = (inputNum == 2 ? inputNum : 1)
operationS = scan.next();
operation = operationS.charAt(0);
switch (operation) {
case 'a':
case 'A':
// perform the addition
break;
case 's':
case 'S':
// perform the subtraction
break;
default:
// print "invalid input" }
import java.text.DecimalFormat;
public class DecimalFormatDemo {
public static void main(String[] args) {
DecimalFormat myPattern = new DecimalFormat("0.00%");
double percent = 0.374;
System.out.println(myPattern.format(percent));
}
}
37.40%
double rand = Math.random()
int rand = 6 + (int) (Math.random() * 20);
import processing.core.*;
public class processingDemo extends PApplet {
private String URL = "https://...";
private PImage backgroundImg;
public void setup() {
size(300,300);
backgroundImg = loadImage(URL,"jpg");
backgroundImg.resize(0, height);
}
public void draw() {
image(backgroundImg,0,0);
fill(255,209,0);
ellipse(width/4,height/4,width/4,height/4);
}
}