Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CS591x
A very brief introduction to Java
Java
Java
Developed by Sun Microsystems
was intended a language for embedded
applications
became a general purpose language
designed to support network programming
Java
Java is a semi-compiled language
Java compiler produces bytecode
Bytecode not executable by itself like a
binary executable (… except…)
Must run Java bytecode in an execution
environment
Java Virtual Machine – JVM
This give Java programs portability
Java
Must download and install Java
Software Developers Kit (SDK)
A lot of kinds of Java out there
You want J2SE 1.4.2 SDK
Download it from java.sun.com
install
You may want a Java IDE
Java - Installation
Installation should be straight-forward
download
open the installer package
see installation instructions on
java.sun.com/j2se
Follow installation instructions/answer
questions
Java
Java SDK comes with a number of tools
Java compiler
javac myprogram.java
compiler produces class files
Java runtime environment (JVM)
java myprog
Java Archiver – jar
jar cvf myjar.jar myprog.class ….
Java – Basic Syntax
public class myprog
{
public static void main(String[] args)
{
System.out.println(“Hello from myprog”);
}
}
Java – Basic syntax
a Java program must start with a class
name
class name must match the program’s
file name
compiler will produce a .class file with
the same name
Java – Basic syntax
Java programs contain classes and
methods
There are Java applications and Java
applets
A Java application must have a main
method
public static void main(String[] args){ … }
Java – Basic Syntax
Classes have zero or more properties
and zero or more methods
properties are variables associated with
class
methods are procedures for operating on
the class, objects or properties
Objects are instances of classes
Java – sample program
public class sample {
public static void main(String[] args)
{
String str;
str = “first string”;
System.out.println(str);
str = “2nd string”;
System.out.println(str);
}
}
Java Data Types
chr – single unicode character
String – string of characters
int – integer
float – floating point number
boolean – boolean value
Java – Data Types
byte – single byte signed integer
short – two byte signed integer
long – very double precision integer
double – double precision float
Java
Arithmetic operators
+ - * / % ++ -use parentheses to set precedence
Logical operators
&& - logical AND || logical OR ! logical NOT
Assignment
int x = 12; String st = “Hello”; float a = 12.5;
a=b; a+=b; a-=b; a*=b; a/=b; a%=b;
Comparison operators
==
!=
>
<
>=
<=
Java Statements
if (comparison-op) { code to be exec;}
if (comparison-op)
{do_this_code;} else
{do_this_code_instead;}
switch(num) {
case 1 : {do_this;}
case 2 : {do_that;}
case 3 : {do_the_other;}
Java Statements
for (initial_val; exit_test; increment)
{iterative_code;}
works just like c
while (test_cond) {
do_this_code;}
do {do_this_code;} while (test_cond);
Java - Statements
break; breaks out of a loop
if (x==3) break;
continue; drops through a loop
if (x==3) continue;
labels
bigloop: for…
continue bigloop;
Java Statements
return – returns to the calling method
can return values from called
methods
Java - statements
Arrays
int[] x = {4,5,6};
String[] days = {“Monday”,”Tuesday”,…};