by nicholas policelli an introduction to java. basic program structure public class classname {...

Click here to load reader

Upload: deirdre-gallagher

Post on 16-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

An Introduction to Java

By Nicholas PolicelliAn Introduction to JavaBasic Program Structurepublic class ClassName { public static void main(String[] args) { program statements } user defined methods }Public classes are accessible from any class. Classes, which are not declared public are accessible only within their package Public methods may be called from any class. Static methods do not operate on objects. The keyword void indicates that the method does not return a value. The array args of the main method stores command line arguments.2Basic Program StructureClasses can be public or privateMethods can be either public or staticPublic methods can be called from any classStatic methods cannot be used on objectsVoid indicates that the method does not return a valueThe array args stores command line argumentsBasic Program StructureThe 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 TypesIntegers: int, short, long, byte

Floating-Point Types: float, double

The Character Type: char

The Boolean Type: boolean (values: true, false)Operators and StringsArithmetic Operators: +, -, *, /, %

Relational Operators: ==, !=, , =

Logical Operators: &&, ||, !

Strings are standard Java class. Concatenation uses +VariablesVariables 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++ conventionsArraysStandard class in JavaDeclared 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 arrayAs in C++ and Perl array indices run from 0 up to the length of the array minus onearrayName.length can be used to find the length of the array

Array Exampleint n=5; int[] numbers = new int[n]; for(int i=0; i