|
|
|
|
|
|
|
boolean
byte
char
short, int, long
float, double
Object
String
arrays, classes, interfaces
int x = 123;
String y = Integer.toString(x);
y = "456";
x = Integer.parseInt(y);
double z = 3.5;
x = (int) z;
|
bool
byte, sbyte
char
short, ushort, int, uint, long, ulong
float, double
structures, enumerations
object
string
arrays, classes, interfaces, delegates
int x = 123;
String y = x.ToString();
y = "456";
x = System.Convert.ToInt32(y);
double z = 3.5;
x = (int) z;
|
|
|
| final double PI = 3.14; |
const double PI = 3.14; |
|
|
public class Action {
public static final int START = 0;
public static final int STOP = 1;
public static final int REWIND = 2;
public static final int FORWARD = 3;
}
int action = Action.STOP;
System.out.println(action);
|
enum Action {Start, Stop, Rewind, Forward};
enum Status {Flunk = 50, Pass = 70, Excel = 90};
Action a = Action.Stop;
if (a != Action.Start)
Console.WriteLine((int)
a);
Console.WriteLine(Status.Pass); |
|
|
|
== < > <= >= !=
+ - * /
%
/
Math.Pow(x, y)
= += -= *= /= %= &= |=
^= <<= >>= >>>= ++ --
& | ^ ~ << >>
>>>
&& || & | ^ !
Note: && and || perform short-circuit logical
evaluations
+
|
== < > <= >= !=
+ - * /
%
/
Math.Pow(x, y)
= += -= *= /= %= &= |=
^= <<= >>= ++ --
& | ^ ~ << >>
&& || & | ^ !
Note: && and || perform short-circuit logical
evaluations
+
|
|
|
java.io.DataInput in = new
java.io.DataInputStream(System.in);
System.out.print("What is your name? ");
String name = in.readLine();
System.out.print("How old are you? ");
int age = Integer.parseInt(in.readLine());
System.out.println(name + " is " + age + " years old.");
int c = System.in.read();
System.out.println(c);
|
Console.Write("What's your name? ");
string name = Console.ReadLine();
Console.Write("How old are you? ");
int age = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("{0} is {1} years old.", name, age);
Console.WriteLine(name + " is " + age + " years old.");
int c = Console.Read();
Console.WriteLine(c);
|
|
|
int Add(int x, int y) {
return x + y;
}
System.out.println("2 + 3 = " + Add(2, 3));
|
void PrintSum(int x, int y) {
System.out.println(x + y);
}
System.out.print("2 + 3 = ");
PrintSum(2, 3);
|
void TestFunc(int x, Point p) {
x++;
p.x++;
p = null;
}
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) );
|
int Add(int x, int y) {
return x + y;
}
Console.WriteLine("2 + 3 = " + Add(2, 3));
|
void PrintSum(int x, int y) {
Console.WriteLine(x + y);
}
Console.Write("2 + 3 = ");
PrintSum(2, 3);
|
void TestFunc(int x, ref int y, out int z,
Point p1, ref Point p2) {
x++; y++; z = 5;
p1.x++;
p1 = null;
p2 = null;
}
class Point {
public int x, y;
}
Point p1 = new Point();
Point p2 = new Point();
p1.x = 2;
int a = 1, b = 1, c;
TestFunc(a, ref b, out c, p1, ref
p2);
Console.WriteLine("{0} {1} {2} {3} {4}",
a, b, c, p1.x, p2 == null);
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
|
|
|
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;
|
|
|
|
while (i < 10)
i++;
for (i = 2; i < = 10; i += 2)
System.out.println(i);
do
i++;
while (i < 10);
import java.util.Vector;
Vector list = new Vector();
list.add(new Integer(10));
list.add("Bisons");
list.add(new Float(2.3));
for (int i=0; i < list.size(); i++)
System.out.println(list.elementAt(i));
String[] names = {"Fred", "Sue", "Barney"};
for (int i=0; i < names.length; i++)
System.out.println(names[i]);
|
while (i < 10)
i++;
for (i = 2; i < = 10; i += 2)
Console.WriteLine(i);
do
i++;
while (i < 10);
// 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);
string[] names = {"Fred", "Sue", "Barney"};
foreach (string s in names)
Console.WriteLine(s);
|
|
|
|
greeting = age < 20 ? "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, or char
case 1:
x++;
// Falls through to next case if no break
case 2: y++; break;
case 3: z++; break;
default: other++;
}
|
greeting = age < 20 ? "What's up?" : "Hello";
if (x < y)
Console.WriteLine("greater");
if (x != 100) {
x *= 5;
y *= 2;
}
else
z *= 6;
string color = "red";
switch (color) {
// Can be 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
}
|
|
|
|
String school = "Harding ";
school = school + "University";
String mascot = "Bisons";
if (mascot == "Bisons")
if (mascot.equals("Bisons"))
if (mascot.equalsIgnoreCase("BISONS"))
if (mascot.compareTo("Bisons") == 0)
System.out.println(mascot.substring(2, 5));
StringBuffer buffer = new StringBuffer("two
");
buffer.append("three ");
buffer.insert(0, "one ");
buffer.replace(4, 7, "TWO");
System.out.println(buffer);
|
string school = "Harding ";
school = school + "University";
string mascot = "Bisons";
if (mascot == "Bisons")
if (mascot.Equals("Bisons"))
if (mascot.ToUpper().Equals("BISONS"))
if (mascot.CompareTo("Bisons") == 0)
Console.WriteLine(mascot.Substring(2, 3));
System.Text.StringBuilder buffer = new System.Text.StringBuilder("two
");
buffer.Append("three ");
buffer.Insert(0, "one ");
buffer.Replace("two", "TWO");
Console.WriteLine(buffer);
|
|
|
|
Exception ex = new Exception("Something is really wrong.");
throw ex;
try {
y = 0;
x = 10 / y;
} catch (Exception ex) {
System.out.println(ex.getMessage());
} finally {
}
|
Exception up = new Exception("Something is really wrong.");
throw up;
try {
y = 0;
x = 10 / y;
} catch (Exception ex) {
Console.WriteLine(ex.Message);
} finally {
}
|
|
|
|
class FootballGame extends Competition {
...
}
interface IAlarmClock {
...
}
interface IAlarmClock extends IClock {
...
}
class WristWatch implements IAlarmClock,
ITimer {
...
}
|
class FootballGame : Competition {
...
}
interface IAlarmClock {
...
}
interface IAlarmClock : IClock {
...
}
class WristWatch : IAlarmClock, ITimer {
...
}
|
|
|
|
package harding.compsci.graphics;
import harding.compsci.graphics.Rectangle;
import harding.compsci.graphics.*;
|
namespace Harding.Compsci.Graphics {
...
}
namespace Harding {
namespace Compsci {
namespace Graphics {
...
}
}
}
using Harding.Compsci.Graphics;
|
|
|
|
class SuperHero {
private int mPowerLevel;
public SuperHero() {
mPowerLevel = 0;
}
public SuperHero(int powerLevel) {
this.mPowerLevel= powerLevel;
}
protected void finalize() throws Throwable {
super.finalize();
}
}
|
class SuperHero {
private int mPowerLevel;
public SuperHero() {
mPowerLevel = 0;
}
public SuperHero(int powerLevel) {
this.mPowerLevel= powerLevel;
}
~SuperHero() {
}
}
|
|
|
No structs in Java.
|
struct StudentRecord {
public string name;
public float gpa;
public StudentRecord(string name, float gpa) {
this.name = name;
this.gpa = gpa;
}
}
StudentRecord stu = new StudentRecord("Bob", 3.5f);
StudentRecord stu2 = stu;
stu2.name = "Sue";
Console.WriteLine(stu.name);
Console.WriteLine(stu2.name); |
|
|
|
private int mSize;
public int getSize () { return mSize; }
public void setSize (int value) {
if (value < 0)
mSize = 0;
else
mSize = value;
}
int s = shoe.getSize();
shoe.setSize(s+1);
|
private int mSize;
public int Size {
get { return mSize; }
set {
if (value < 0)
mSize = 0;
else
mSize = value;
}
}
shoe.Size++;
|