Java Programming
CIS279
Tori Basford
tori@bly.net
Assignment(s):
Send email to tori@bly.net.
Exercise(s):
See handout (Exercise 1: Compiling and running a simple Java program)
Java Basics:
Unlike C/C++, Java is processed by software rather than hardware. The java source code (file.java) is processed by the compiler, which compiles it into a class file (file.class). The class file is run by the Java Virtual Machine. Programs that are downloaded and run across the internet are called 'Applets.'
Java Versions:
The 8 Primary Data Types:
| Name: | Size: | Type: |
| byte | 8 | integer |
| short | 16 | integer |
| int | 32 | integer |
| long | 64 | integer |
| float | 32 | floating point |
| double | 32 | floating point |
| char | - | character |
| boolean | - | boolean (true or false) |
Declaring a variable:
Variables are declared by first saying the variable type, than the variable name.
examples:
int a;
boolean b;
char c;
double d;
Setting variables:
Setting variables is done with the equal (=) operator.
examples:
a = 122;
b = true; b = false; b = 12 < 3;
c = 'A';
d = 12E1; d = 122.95 ;
In cases of trying to set one variable to another:
Java will allow doubles to be set with integers (d=a), but will only allow integers to be set as doubles if the (int) is used.
examples:
a = (int) 122.95;
a = (int) d;
"Adding" Strings:
When two variables are added, the resulting variable is set to the larger data type. ie, if a float and a integer are added, the result is a float. Between two strings, the + is used to concatanate. If a string is added to a integer, the result is a string.
examples:
"cat" + "dog" = "catdog"
"cat" + 12 + 27 = "cat12" + 27 = "cat1227"
12 + 29 + "cat" = 39 + "cat" = "39cat"
Printing to screen:
To print to the screen, the compiler has to convert the data to a string of text, then print the text.
The System.out.println() function can print any single item.
examples:
int a=145;
boolean b=false;
double d=37.12;
System.out.println(a);
145
System.out.println(b);
false
System.out.println(a+d);
182.12
System.out.println(a+b);
error
System.out.println("cat"+a);
cat145
System.out.println("cat"+b);
catfalse
System.out.print does the same as the "println" function, but without the newline character.
Objects:
An Object is a collection of methods and data.
A Class is a "factory" that can create objects.
examples:
There is a class Pima Student which can create PimaStudent objects.
To do this:
Do this:
Create a variable a to contain an integer.
int a;
Create an int and store into a
a=47;
Create a variable S1 to contain a PimaStudent object
PimaStudent S1;
Create a PimaStudent and store into S1
S1 = new PimaStudent();
Create S2
PimaStudent S2;
-
S2 = new PimaStudent("Tom", 45);
Print S2
System.out.println(S2.name);
-
System.out.println(S2.age);
Make S2 1 year older
S2.age++;
Each object has a method pr() that will print it. ie, S2.pr();
Function signatures:
The signature of a function is what kind of data it wants. Multiple functions of the same name with different signatures are allows. Multiple functions with alike signatures are NOT allowed.
For example, there are five ways to create the "PimaStudent" object:
Creating Arrays:
Arrays are created via brackets ( [ ] ).
example:
int[] r;
Creating Strings:
Strings are objects. They are created with the String class.
example:
String s;
The Java Main Program
Indentation:
Brackets are put at the end of a line. After a bracket, the following lines are indented four spaces.
example:
public class Prog1 {
    public static void main(string[] args){
    }
}