Basics of Java Programming
Basics of Java Programming
Basics of Variables and Assignment Like almost every programming language, Java has variables and an assignment statement to put values into a variable. There are two classifications of variables in Java: primitive types and reference variables. The primitive types we are interested in are int, double, and Boolean, for integer, floating point, and true/false values respectively. An example of declaring and assigning to primitive types: int x; x = 3; double y; y = 0.3; boolean b; b = (x < y); // // // // // // // declare an integer variable x assign the value 3 to x declare a floating point variable y assign the value 0.3 to y declare a boolean variable b b will be assigned false, because the expression x < y evaluates to false
A reference variable either points to an object or is null (null is a keyword for a reference variable that is empty). An example of declaring and using reference variables: Foo f1; Foo f2; f1 = new Foo(Im a foo!); // create a new Foo and have f1 // point to it f2 = f1; // f2 now points to the same Foo as f1 f1 = null; // now f1 doesnt point to anything but // f2 still points to the Foo created before
Basics of Classes Java is an object-oriented programming (OOP) language and the fundamental unit for organizing code is the class. A class has properties (a.k.a. member data) which contain characteristics and data (in general, nouns or adjectives) and methods (code) for actions (verbs). Properties and methods together are called, collectively, members. Here is an example of a partial class for holding a complex number in rectangular form. public class RectComplexNumber { double a; // the real part of the complex number double b; // the complex (i) part of the complex number // constructorcreate a new complex number with given values public RectComplexNumber(double aInput, double bInput) { a = aInput; b = bInput; } // add this complex number to addend and return the result public RectComplexNumber add(RectComplexNumber addend) { return new RectComplexNumber(a + addend.a, b + addend.b); } // etc. }
Using Built-In Classes and Packages The Java Developers Kit (JDK) comes with a huge amount of built-in Java code that can be used to build applications. However, for a Java class to use this code it must identify the package that contains that code. This is done at the top of the source file using the import keyword. Example: import java.util.*; import java.io.File; Public vs. Private Like all OOP languages, Java has mechanisms to support encapsulation. In general, we declare as public all properties and methods we want code outside our package to use. Otherwise the default is that all code in the same package can access the members. If we only want the code in that particular class to access members, we declare them as private. Good encapsulation style suggests that all properties have non-public or private access. In other words, the outside world should only be able to access or modify properties by calling methods. There, are, however, exceptions. Basic Flow of Control There are two main flow-of-control constructs: branches (if-then) and loops. The Java syntax for a branch is if (x > 10000) { System.out.println(x is big!); } else if (x > 100) { System.out.println(x is medium.); } else { System.out.println(x is small.); } Note that the code inside the curly brackets for each if or else can be arbitrarily complex and thus might contain additional branches. // // // // gain access to all the code in the java.util package (* means all) gain access to the File class of the java.io package
Below is a code fragment that illustrates the basic Java syntax for a loop: int i; // loop variable int total; // accumulator // this loop adds up the integers from 1 to MAX for ( i = 1; i++; i <= MAX) { total = total + i; }