Presentation is loading. Please wait.

Presentation is loading. Please wait.

By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.

Similar presentations


Presentation on theme: "By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements."— Presentation transcript:

1 By Nicholas Policelli An Introduction to Java

2 Basic Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods }

3 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

4 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)

5 Data Types Integers: int, short, long, byte Floating-Point Types: float, double The Character Type: char The Boolean Type: boolean (values: true, false)

6 Operators and Strings Arithmetic Operators: +, -, *, /, % Relational Operators: ==, !=,, = Logical Operators: &&, ||, ! Strings are standard Java class. Concatenation uses +

7 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);

8 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

9 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

10 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]);

11 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

12 Class Definitions The basic structure of Java classes: class NameOfClass { constructors methods fields }

13 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.

14 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.

15 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

16 References Example classes and more can be found in the Java resources on the course website


Download ppt "By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements."

Similar presentations


Ads by Google