Skip to content

Instantly share code, notes, and snippets.

@suntong
Created December 25, 2013 20:50
Show Gist options
  • Save suntong/8126818 to your computer and use it in GitHub Desktop.
Save suntong/8126818 to your computer and use it in GitHub Desktop.
"two\nlines"
"\"This is in quotes\""
int d = 3, e, f = 5;
byte z = 22;
double pi = 3.14159;
char x = 'x';
z = (byte) f;
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
// final is similar to const in C/C++/C#
final int FILE_NEW = 1;
b = (byte) i;
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
int twoD45[][]= new int[4][5];
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
double m[][] = {
{ 0*0, 1*0, 2*0, 3*0 },
{ 0*1, 1*1, 2*1, 3*1 },
{ 0*2, 1*2, 2*2, 3*2 },
{ 0*3, 1*3, 2*3, 3*3 }
};
System.out.println("lengths are: " + month_days.length + " " + twoD45.length + " " + twoD.length);
if(i == 10) {
if(j < 20) a = b;
d++;
}
else if(i == 11)
i++;
else a = d; // this else refers to if(i == 10)
switch(i) {
case 0:
System.out.println("i is zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 3:
case 4:
default:
System.out.println("i is greater than one.");
}
}
int n = 20;
while(n > 10) {
System.out.println("tick " + n);
n--;
}
do {
System.out.println("tick " + n);
n--;
} while(n > 0);
for(int a=1, int b=4; a<b; a++, b--) {
System.out.println("a = " + a);
System.out.println("b = " + b);
}
for(int n=10; n>0; n--)
System.out.print(n + " ");
if(n == 6) break; // terminate loop
if(n%2 == 0) continue;
System.out.println("");
}
first: {
second: {
third: {
System.out.println("Before the break.");
if(t) break second; // break out of second block
System.out.println("This won't execute");
}
System.out.println("This won't execute");
}
System.out.println("This is after second block.");
}
}
// Demonstrating some String methods.
class StringDemo2 {
public static void main1(String args[]) {
String strOb1 = "First String";
String strOb2 = "Second String";
String strOb3 = strOb1;
System.out.println("Length of strOb1: " +
strOb1.length());
System.out.println("Char at index 3 in strOb1: " +
strOb1.charAt(3));
if(strOb1.equals(strOb2))
System.out.println("strOb1 == strOb2");
else
System.out.println("strOb1 != strOb2");
if(strOb1.equals(strOb3))
System.out.println("strOb1 == strOb3");
else
System.out.println("strOb1 != strOb3");
}
public static void main2(String args[]) {
String str[] = { "one", "two", "three" };
for(int i=0; i<str.length; i++)
System.out.println("str[" + i + "]: " +
str[i] + ". Length: " str[i].length());
}
}
class Box {
double width;
double height;
double depth;
// This is the constructor for Box.
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
// compute and return volume
double volume() {
return width * height * depth;
}
protected void finalize( )
{
System.out.println("finalized.");
}
}
// Here, Box is extended to include weight.
class BoxWeight extends Box {
double weight; // weight of box
// initialize width, height, and depth using super()
BoxWeight(double w, double h, double d, double m) {
super(w, h, d); // call superclass constructor
weight = m;
}
}
The subclass BoxWeight is used as a superclass to create the subclass called
Shipment. Shipment inherits all of the traits of BoxWeight and Box, and adds
a field called cost, which holds the cost of shipping such a parcel.
Box mybox; // declare reference to object
mybox = new Box(3, 6, 9); // allocate a Box object
When a simple type is passed to a method, it is done by use of
call-by-value. Objects are passed by use of call-by-reference.
public static int divide(int[] array, int index) {
try {
System.out.println("\nFirst try block in divide() entered");
array[index + 2] = array[index]/array[index + 1];
System.out.println("Code at end of first try block in divide()");
return array[index + 2];
} catch(Exception e) {
System.err.println("Exception caught in divide()\n" +
"\nMessage in exception object:\n\t" +
e.getMessage());
System.err.println("\nStack trace output:\n");
e.printStackTrace();
} finally {
System.err.println("finally clause in divide()");
}
System.out.println("Executing code after try block in divide()");
return array[index + 2];
}
// Display all command-line arguments.
class CommandLine {
public static void main(String args[]) {
for(int i=0; i<args.length; i++)
System.out.println("args[" + i + "]: " +
args[i]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment