Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
An Introduction to Java
By Nicholas Policelli
Basic Program Structure
public class ClassName
{
public static void main(String[] args)
{
program statements
}
user defined methods
}
Basic Program Structure
Classes can be public or private
Methods can be either public or static
Public methods can be called from any class
Static methods cannot be used on objects
Void indicates that the method does not return a value
The array args stores command line arguments
Basic Program Structure
The source code must have the same name as the public class
and have a .java
During compilation the Java compiler produces a bytecode
file which is given a .class extension
Execution: java (class name)
Data Types
Integers: int, short, long, byte
Floating-Point Types: float, double
The Character Type: char
The Boolean Type: boolean (values: true, false)
Operators and Strings
Arithmetic Operators: +, -, *, /, %
Relational Operators: ==, !=, <, >, <=, >=
Logical Operators: &&, ||, !
Strings are standard Java class.
Concatenation uses +
Variables
Variables in Java need to be declared.You need to initialize
variables before you use them.
Examples:
int n=5;
System.out.println(n);
String s=”Hello”;
String t=”world”;
System.out.println(s+” “+t);
If, If Else, For ,and While Loops
If and if else follow the C++ conventions.
if(…)
{
…
}
While and For loops also follow the same C++ conventions
Arrays
Standard class in Java
Declared by specifying the type enclosed in []
All elements may be listed by enclosing in {} and separated
with commas.
If one array variable is copied into another array variable,
both variables refer to the same array
As in C++ and Perl array indices run from 0 up to the length
of the array minus one
arrayName.length can be used to find the length of the array
Array Example
int n=5;
int[] numbers = new int[n];
for(int i=0; i<numbers.length; i++)
numbers[i]=n-i;
Arrays.sort(numbers);
for(int i=0; i<numbers.length; i++)
System.out.println(numbers[i]);
Object Oriented Programming in Java
Program structure with user defined classes
The basic structure of Java programs with user defined classes:
public class ClassName
{
public static void main(String[] args)
{
program statements
}
user defined methods
}
user defined classes
Class Definitions
The basic structure of Java classes:
class NameOfClass
{
constructors
methods
fields
}
Objects and Object Variables
Java constructors construct and initialize objects.
Constructors have the same name as the class.
Objects are constructed by an application of a constructor
and new.
Object variables store the reference to the object, not the
object itself.
Example:
Date D=new Date(); // An object of a Date class is
constructed and its reference
System.out.println(D); // is stored inside variable D. Next we
print out the object.
Class Fields and Methods
Class fields define the state of an object.
Constructors define and initialize class fields.
Accessor methods access class fields of an object.
Mutator methods are used to modify class fields.
Parameter Passing
Two kinds of method parameters
Primitive Types
Object References
In both cases the variable is passed by value,
For primitive types the parameter will contain the primitive
type
For Object References the parameter will contain the
reference, not the object
References
Example classes and more can be found in the Java resources
on the course website