|
|
Single
/* Multiple
Line * *
/** Javadoc Documentation Comments * * |
Single
/* Multiple
Line * *
XML comments on a single
/** XML comments on multiple lines * * |
|
|
|
Primitive Types
Boolean
byte
char
short, int, long
float, double
Reference Types
object (superclass of all other classes)
String
Arrays, classes, interfaces
Conversions
//int to String
int x = 123;
String y = integer.tostring (x); //Y is "123"
//String to int
y = "456";
x = Integer.parseint (y); //X are 456
// double to Intdouble z = 3.5;
x = (int) z; //x is 3 (truncates decimal) |
Value Types
bool
BYTE, sbyte
Char
Short, ushort, int, uint, long, ulong
float, double, decimal
Structures, enumerations
Reference Types
Object (superclass of all other classes)
String
arrays, classes, interfaces, delegates
Convertions
int to String
int x = 123;
String y = x.tostring (); Y is "123"
string to int
y = "456";
x = Int. Parse (y); or x = Convert.ToInt32 (y);
Double to int
Double z = 3.5;
x = (int) z; X is 3 (truncates decimal) |
|
|
May is initialized in a constructor final Double PI = 3.14; |
Const Double PI = 3.14;
Can is set to a const or a variable. May is initialized in a constructor. readonly int max_height = 9; |
|
|
|
enum Action {Start, Stop, Rewind, Forward};
//Special type of Class enum Status {
flunk (m), pass (in), Excel (in);
private final int value;
Status (int value) {this.value = value;}
public int value () {return value;}
};
Action a = action.stop;
if (a!= action.start)
System.out.println (a); //Prints "Stop"
Status s = status.pass;
System.out.println (S.value ()); //Prints |
enum Action {Start, Stop, Rewind, Forward};
enum Status {flunk = +, pass = N, Excel = 90};
No equivalent.
Action a = Action.stop;
if (a!= action.start)
Console.WriteLine (a); Prints "Stop"
Status s = status.pass;
Console.WriteLine ((int) s); Prints "70" |
|
|
|
Comparison
== < > <= >= !=
Arithmetic
+ - *& nbsp /
% (MoD)
/ (integer division If both operands are ints)
Math.pow (x, y)
Assignment
= += = *= /= %= &= |= ^= <<= >>= > >>= ++ --
Bitwise
& | ^ ~ << >>& nbsp >>>
Logical
&& | | & | ^ !
Note: && and | | perform short-circuit logical evaluations
String concatenation
+ |
Comparison
= = < > <= >=!=
Arithmetic
+ - * /
% (MoD)
/(integer division If both operands are ints)
Math.pow (x, y)
Assignment
= + = = *=/=%= &= |= ^= <<= >>= + +--
Bitwise
& | ^ ~ << >>
Logical
&& | | & | ^ !
Note: && | | Perform short-circuit logical evaluations
String concatenation
+ |
|
|
|
Greeting = Age < ? "What ' s up?" : "Hello";
if (x < y)
System.out.println ("greater");
if (x!= 100) {
x *= 5;
Y *= 2;
} Else
Z *= 6;
int selection = 2; Switch (selection) {//Must be-byte, short, int, char, or enum case 1:x++; Falls through to next if no break case 2:y++; Break ; case 3:z++; Break ; default: other++;
} |
Greeting = Age < ? "What ' s up?" : "Hello";
If (x < y)
Console.WriteLine ("Greater");
If (x!=) {
x *= 5;
y *= 2;
} else
Z *= 6;
String color = "Red"; Switch (color) { /Can is any predefined type
Case "Red": r++; break; //break is mandatory; No Fall-through
case "Blue": b++; break;
Case "green": g++; break;
Default: other++; break; //Break necessary on default
}
|
|
|
|
while (I < 10)
i++;
for (i = 2; I <= i + + 2)
System.out.println (i);
Todo
i++; while (I < 10);
for (int i : numarray)//foreach Construct
sum + = i;
For loop can is used to iterate through any Collection
Import java.util.ArrayList;
arraylist<object> list = new arraylist<object> ();
List.add (10); Boxing converts to instance of Integer
List.add ("Bisons");
List.add (2.3); Boxing converts to instance of Double
for (Object o : list)
System.out.println (o); |
while (I < 10)
i++;
for (i = 2; I <= i + + 2)
Console.WriteLine (i);
Todo
i++; while (I < 10);
foreach (int i in Numarray)
sum + = i;
foreach can be used to iterate through any collection
Using System.Collections;
ArrayList list = new ArrayList ();
List. ADD (10);
List. ADD ("Bisons");
List. ADD (2.3);
foreach (Object o in list)
Console.WriteLine (o); |
|
|
int nums [] = {1, 2, 3}; or int [] nums = {1, 2, 3 };
for (int i = 0; i < nums.length i++)
System.out.println (nums[i]);
String names[] = new STRING[5];
Names[0] = "David";
Float Twod [[] = new Float[rows][cols];
TWOD[2][0] = 4.5;
int [[] jagged = new int[5][];
Jagged[0] = new INT[5];
Jagged[1] = new INT[2];
Jagged[2] = new INT[3];
Jagged[0][4] = 5; |
int [] nums = {1, 2, 3};
for (int i = 0; i < Nums. Length; i++)
Console.WriteLine (Nums[i]);
string[] names = new String[5];
Names[0] = "David";
Float [,] Twod = new float[rows, cols];
twod[2,0] = 4.5f;
int [[] jagged = new int[3][] {
new int[5], new int[2], new int[3]};
Jagged[0][4] = 5; |
|
|
Return single value int ADD (int x, int y) { return x + y;
}
int sum = ADD (2, 3); |
Return no value void Printsum (int x, int y) {
System.out.println (x + y);
}
Printsum (2, 3); |
Primitive types and references are always passed by value
void TestFunc (int x, point p) {
x + +;
p.x++; Modifying property of the object
p = null; Remove Local reference to Object
}
Class Point {
public int x, y;
}
Point P = new Point ();
p.x = 2;
int a = 1;
TestFunc (A, p);
System.out.println (A + "" + p.x + "" + (P = null)); 1 3 False
Accept variable number of arguments
int Sum (int ... nums) {
int sum = 0;
for (int i:nums)
sum + = i;
return sum;
}
int total = Sum (4, 3, 2, 1); Returns 10
|
Return single value int ADD (int x, int y) { return x + y;
}
int sum = ADD (2, 3); |
Return no value void Printsum (int x, int y) {
Console.WriteLine (x + y);
}
Printsum (2, 3); |
Pass through value (default), In/out-reference (ref), and out-reference (out)
void TestFunc (int x, ref int y, out int Z, point p1, ref point p2) {
x + +; y++; z = 5;
p1.x++; Modifying property of the object
P1 = null; Remove Local reference to Object
P2 = null; Free the object
}
Class Point {
public int x, y;
}
Point P1 = new Point ();
Point P2 = new Point ();
p1.x = 2;
int a = 1, b = 1, C; Output param doesn ' t need initializing
TestFunc (A, ref B, out C, p1, ref p2);
Console.WriteLine ("{0} {1} {2} {3} {4}"),
A, B, C, p1.x, p2 = null); 1 2 5 3 True
Accept variable number of arguments
int Sum (params int[] nums) {
int sum = 0;
foreach (int i in nums)
sum + = i;
return sum;
}
int total = Sum (4, 3, 2, 1); Returns 10
|
|
|
|
String concatenation String School = "Harding";
School = school + "University"; School is "Harding University"
String comparison
String mascot = "Bisons";
if (mascot = = "Bisons")//not the correct way todo string comparisons
if (mascot. equals("bisons"))//True
if (mascot. Equalsignorecase("bisons"))//True
if (mascot. CompareTo("bisons") = = 0)//True
System.out.println (mascot. Substring(2, 5)); Prints "Son"
My Birthday:oct 12, 1973
Java.util.Calendar C = new Java.util.GregorianCalendar (1973, 10, 12);
String s = String.Format ("My Birthday:%1$tb%1$te,%1$ty", c);
mutable string stringbuffer buffer = new StringBuffer("two");
Buffer. Append ("three");
Buffer. Insert (0, "one");
Buffer. Replace (4, 7, "two");
SYSTEM.OUT.PRINTLN (buffer); Prints "One Two three"
|
String concatenation string school = "Harding";
School = school + "University"; School is "Harding University"
String comparison
String mascot = "Bisons";
if (mascot = = "Bisons")//True
if (mascot. Equals("bisons"))//True
if (mascot. ToUpper(). Equals ("Bisons")) True
if (mascot. CompareTo("bisons") = = 0)//True
Console.WriteLine (mascot. Substring(2, 3)); Prints "Son"
My Birthday:oct 12, 1973
DateTime dt = new DateTime (1973, 10, 12);
string s = "My Birthday:" + dt. ToString ("MMM dd, yyyy");
mutable string
System.Text. StringBuilder buffer = new System.Text. StringBuilder ("two");
Buffer. Append ("three");
Buffer. Insert (0, "one");
Buffer. Replace ("Two", "two");
Console.WriteLine (buffer); Prints "One Two three"
|
| Java |
Exception Handling |
C# |
|
|
//Must be in a method that are declared to throw this exception
Exception ex = new Exception ("Somet Hing is really wrong. "); throw ex;
Try {
y = 0;
x = 10/y;
} Catch (Exception ex) {
System.out.println (Ex.getmessage ());
} Finally {
//Code that always gets executed
} |
Exception up = new Exception ("Some Thing is really wrong. "); throw up; /ha ha
Try {
y = 0;
x = 10/y;
} Catch (Exception ex) { //Variable "Ex" is optional
Console.WriteLine (ex. message);
} finally {
//Code that always gets executed
} |
|
|
|
package Harding.compsci.graphics;
Import Harding.compsci.graphics.Rectangle; Import Single class
import harding.compsci.graphics.*; Import All Classes |
namespace Harding.Compsci.Graphics {
...
}
Or
namespace Harding { namespace CompSci { namespace Graphics {
...
}
}
}
Import all class. Can ' t import single class. using Harding.Compsci.Graphics; |
|
|