|
package hello;
public class HelloWorld { public static void main(String[] args) { String name = "Java";
|
using System;
namespace Hello { public class HelloWorld { public static void Main(string[] args) { string name = "C#";
|
|
|
|
|
|
|
|
|
const double PI = 3.14;
|
|
enum Action {Start, Stop, Rewind, Forward};
Action a = Action.Stop; if (a != Action.Start) System.out.println(a);
|
enum Action {Start, Stop, Rewind, Forward};
enum Status {Flunk = 50, Pass = 70, Excel = 90};
Status s = Status.Pass; Console.WriteLine((int) s);
|
|
Note: && and || perform short-circuit logical evaluations
|
Note: && and || perform short-circuit logical evaluations
|
|
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, char, or enum 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 }
|
|
while (i < 10) i++;
for (i = 2; i <= 10; i += 2) System.out.println(i);
do i++; while (i < 10);
for (int i : numArray)
|
while (i < 10) i++;
for (i = 2; i <= 10; i += 2) Console.WriteLine(i);
do 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;
|
|
int sum = Add(2, 3);
|
PrintSum(2, 3);
|
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 total = Sum(4, 3, 2, 1);
|
int sum = Add(2, 3);
|
PrintSum(2, 3);
|
class Point { public int x, y; }
Point p1 = new Point(); Point p2 = new Point(); p1.x = 2; int a = 1, b = 1, c;
int total = Sum(4, 3, 2, 1); // returns 10
|
|
System.out.println(mascot.substring(2, 5));
|
Console.WriteLine(mascot.Substring(2, 3));
|
|
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) {
|
|
package harding.compsci.graphics;
|
namespace Harding.Compsci.Graphics { ... }
namespace Harding { namespace Compsci { namespace Graphics { ... } } }
|
|
|
|
|
class SuperHero { private int mPowerLevel;
public SuperHero() { mPowerLevel = 0; }
public SuperHero(int powerLevel) { this.mPowerLevel= powerLevel; }
|
class SuperHero { private int mPowerLevel;
public SuperHero() { mPowerLevel = 0; }
public SuperHero(int powerLevel) { this.mPowerLevel= powerLevel; }
~SuperHero() {
|
|
SuperHero hero = new SuperHero();
hero.setName("SpamMan"); hero.setPowerLevel(3);
hero.Defend("Laura Jones"); SuperHero.Rest();
SuperHero hero2 = hero;
if (hero == null) hero = new SuperHero();
Object obj = new SuperHero(); System.out.println("object‘s type: " + obj.getClass().toString()); if (obj instanceof SuperHero) System.out.println("Is a SuperHero object.");
|
SuperHero hero = new SuperHero();
hero.Name = "SpamMan"; hero.PowerLevel = 3;
hero.Defend("Laura Jones"); SuperHero.Rest();
SuperHero hero2 = hero;
hero = null ;
if (hero == null) hero = new SuperHero();
Object obj = new SuperHero(); Console.WriteLine("object‘s type: " + obj.GetType().ToString()); if (obj is SuperHero) Console.WriteLine("Is a SuperHero object.");
|
|
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++;
|
|
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); |
|
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();
|
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);
int c = Console.Read();
|
|
import java.io.*;
|
using System.IO;
|