Getting started with Java by Vlad Costel Ungureanu for Learn Stuff
Programming Languages A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs to control the behavior of a machine or to express algorithms. (Wikipedia) A specific series of instructions that are interpreted by the machine to produce predictable, repeatable results in a controlled environment. (Business) A sequence of code that produces the expected results. (Programmer) 2
The Java Programming Language Oracle Fast Secure Object oriented Cross platform Over 3 Billion devices Desktop, web and mobile Business accepted frameworks 3
The Java Programming Language public class MyFirstClass { // member data declaration public static void main(string[] args) { } System.out.println("Hello, world!"); // without classes (!) all operations go here // with classes (!) class instantiation and method calls go here // method declaration } 4
5
File Structure Source: HelloWorld.java Compile: javac HelloWorld.java HelloWorld.class Execution: java HelloWorld 6
Syntax Key words: class, new, interface, abstract, public, static, final, private, protected, if, then, else, for, while, break. Literals: Hello World, 10, 010, 0xA, 0b11, 12.3, 12.3d, 12.3f, 12e3, 123L, true, false, null. Separators: ( ) { } [ ] ;,. Operators: + - / % && & >> 7
Data types Primitives: Arithmetic: byte (1), short (2), int (4), long (8) Real: float (4), double (8) Character: char (2) Boolean: (true, false) Reference types: Classes Interfaces Annotations Enum (erations) 8
Variables/Fields A primitive bite: byte a; An primitive integer with the default value of 100 int somevalue= 100; A primitive double constat with the value of PI final double PI = 3.14; A primitive long with a default value long alongnumber= 12345678L; A character array String somestring= any string"; 9
Variables/Fields public class MyFirstClass { int a; // a is a class variable public void asimplemethod(int b) { // b is a method parameter a = b; int c = 10;// c is a variable only visible in the method for(int d=0; d < 10; d++) { // d is a variable visible only in the flow control structure } // control structure end here } // method ends here } // class ends here 10
Arrays Declaration: int[] a; byte b[]; Instantiation: a = new int[10]; char c = new char[100]; Initialization: String colors[] = { Red", Blue, Green }; String colors = new String[]{ Orange", Yellow"}; Size: a.length 11
Arrays Declaration int[][] atwodimensionalarray = new int[10][20]; int[][][] athreedimensionalarray = new int[10][20][30]; Accessing an element: atwodimensionalarray[0][0] = 1; Making a copy System.arrayCopy; 12
Control Structures Simple Logical control: if (boolean value) { // instructions else { } // other instructions Multiple cases logical control: switch (value) { } case 1: result= One"; // instructions break; case 2: result = Two"; // instructions break; default: result = Unknown"; break; 13
Control Structures For for (int i = initial_size; i < max_value; i++){ // instructions } While while (boolean value){ // instructions } Do-while do { // instructions } while (boolean value) 14
Control Structures Exception control: try { // instructions catch (Exception e){ // other instructions } finally { // some other instructions // usually free resources } 15
Control Structures Break Continue Return Throw 16
Equality Arrays: int a[] = {1, 2}; int b[] = {1, 2}; a == b - false a.equals(b) - false Arrays.equals(a,b) - true Characters String s1 = new String("abc"); String s2 = new String("abc"); s1 == s2 - false s1.equals(s2) - true s1.compareto(s2) - 0 17
Assignments For an array of elements count the number of occurrences of a given element. (experiment with arrays of numbers and strings) Read an array and a matrix from the keyboard Use the Random class to generate elements for a matrix for which you read the length and height from the keyboard For two given matrices implement: matrix addition, matrix scalar multiplication and matrix multiplication Generate a random array of numbers and search for a specific one. If you find it then use a named break to exit the loop 18
THANK YOU! Vlad Costel Ungureanu ungureanu_vlad_costel@yahoo.com This is a free course from LearnStuff.ro not for commercial use 19