.java
files into .class
files..class
files can be modified.ClassCastException
: Casting to an incompatible class.NullPointerException
: Using null objects.ArrayIndexOutOfBoundsException
: Illegal array index.StringIndexOutOfBoundsException
: Illegal string index..class
files on a JVM have less access than .exe
files.Sandbox components:
Java API security check example:
Security Manager sm = System.getSecurityManager();
if (sm != null) sm.checkRead(“file.txt”);
Default implementation of a check method
FilePermission perm = new FilePermission(filename, "read");
AccessController.checkPermission(perm);
SecurityManager
implies no restrictions (null check).checkPermission
either returns quietly or throws a SecurityException
.SecurityManager
.System.setSecurityManager(sm)
.checkAccept(String host, int port)
checkAccess(Thread thread)
checkCreateClassLoader()
checkDelete(String filename)
checkRead(FileDescriptor filedescriptor)
checkWrite(FileDescriptor filedescriptor)
checkExec(String command)
checkExit(int status)
checkPropertyAccess(String key, String def)
checkSetFactory()
checkTopLevelWindow(Object window)
if
, else
, do
, while
, for
, switch
, case
, default
, break
, continue
, try
, catch
, finally
, throw
.
, ()
, []
, */%
, +-
, <
, ==
, &&
, ||
, =
bool
(true
, false
), byte
, char
, int
, long
, float
, double
class
, interface
, abstract
, static
, volatile
, new
, null
, this
public
, private
, protected
, void
, return
extends
, implements
become :
super
becomes base
final
becomes readonly
, sealed
import
becomes using
package
becomes namespace {...}
instanceof
becomes is
@Attribute
becomes [Annotation]
(override is C# keyword)using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, world!");
}
}
}
Without Properties:
class C {
private int x;
public int getX(){ return x; }
public void setX(int x){ this.x = x; }
}
C c = new C();
c.setX(5);
int y = c.getX();
With Properties:
class C {
private int x;
public int X { get {return x;} set {x=value;} }
}
C c = new C();
c.X = 5;
int y = c.X;
List<T>
has a !0[]
array. Java uses type erasure.Using statement:
using (FileStream fs = File.OpenRead(path))
using (StreamReader sr = new StreamReader(fs))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
Enum:
enum TrafficLightColour {Red, RedAmber, Green, Amber};
TrafficLightColour tlc = TrafficLightColour.Red;
Foreach loop:
string[] presidents = new string[] { “Washington”, “Jefferson”, “Lincoln”, “Roosevelt” };
foreach (string president in presidents)
{
Console.WriteLine(president);
}
Params keyword:
public static void SumList(params int[] list)
Lambda expressions:
int[] somevalues = { 10, 20, 5, 2, 40, 1 };
int numberOfLargeValues = somevalues.Count(x => x >10);
Example Program with Code Contracts:
using System.Diagnostics.Contracts;
public class BankExample {
public const int MAX = 1000;
private int balance;
public int Balance { get { return balance; } }[ContractInvariantMethod]
private void ObjectInvariant ()
{
Contract.Invariant(balance >= 0 && balance <= MAX);
}
public void Credit(int amount)
{
Contract.Requires(amount > 0);
Contract.Requires(amount + Balance <= MAX);
Contract.Ensures(balance == Contract.OldValue(balance) + amount);
balance += amount;
}
}
Methods of Contract
:
Contract.Requires(bexp)
: Precondition.Contract.Requires<TException>(bexp)
: Precondition with exception type.Contract.Ensures(bexp)
: Postcondition.Contract.EnsuresOnThrow<TException>(bexp)
: Condition for abnormal termination.Contract.Invariant(bexp)
: Invariant expression.Contract.Assert(bexp)
: Assertion.Contract.Assume(bexp)
: Assumption.Contract.OldValue(var)
: Value at method start.Contract.Result()
: Return value.Contract.ForAll
and Contract.Exists
:
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(Contract.ForAll(0,3,i => i > -1));
List<int> list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Console.WriteLine(Contract.ForAll(list, i => i > -1));
Console.WriteLine(Contract.Exists(list, i => i > -1));
}
}
a.out
plus crt0.o
(startup routines) → .exe
file with machine code.eax
, ebx
, ecx
, edx
, esi
, edi
(general purpose).esp
(stack pointer), ebp
(base pointer).mov
), arithmetic/logic (add
), control flow (jmp
, j??
).Original Code:
int z = x + y;
int z = x + y;
JVM Bytecode:
iload_0
iload_1
iadd
istore_2
CLR’s CIL (Common Intermediate Language):
ldloc.0
ldloc.1
add
stloc.2
Local variables in registers are pushed, manipulated, and popped from an operand stack.
Code is sandboxed based on origin for security.
Compiled Java bytecode passes verification.
Modified bytecode can cause verification failures.
Example:
Original Java code:
int i = 0;
if (i==0){
i=1;
}
Original Bytecode:
0: iconst_0
1: istore_1
2: iload_1
3: ifne 8
6: iconst_1
7: istore_1
8: return
Altered Bytecode:
0: iconst_0
1: istore_1
2: iload_1
3: ifne 8
6: fconst_1
7: fstore_1
8: return
Stackmap indicates local variable 1 holds an integer at line 8.
java.lang.VerifyError: Instruction type does not match stack map
Type float (current frame, locals[1]) is not assignable to integer (stack map, locals[1])
new
, putfield
, getfield
, invokevirtual
, ireturn
, newarray
, iaload
, iastore
, d2f
.System.Collections
: ICollection
, IList
, IDictionary
, ArrayList
, Array
, BitArray
, Queue
, Stack
, Hashtable
, SortedList
.System.IO
: FileStream
, NetworkStream
, StreamReader
, StreamWriter
, Directory
, File
.System.Threading
System.Net
: IPAddress
, Socket
, WebRequest
, WebResponse
.System.Reflection
: FieldInfo
, MethodInfo
, ConstructorInfo
.System.Windows.Forms
: Control
, Form
.System.XML
: XMLNode
, XMLDocument
..exe
or .dll
files..class
files for each class (including inner classes) and packages them into .jar
files.C++/CLI
(system back-end), VB.NET
(user interface), F#
(concurrency).DCOM
and CORBA
.