Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java Course 2: Basics

4,918 views

Published on

Lecture 2 from the IAG0040 Java course in TTÜ.
See the accompanying source code written during the lectures: https://github.com/angryziber/java-course

Published in: Technology
  • Be the first to comment

Java Course 2: Basics

  1. 1. Java course - IAG0040 Java Basics, Program FlowAnton Keks 2011
  2. 2. Java type system ● static type checking ● provides (benefits) – by compiler, type – safety declarations required ● many bugs are type ● strongly typed errors – runtime checking – documentation – automatic conversions – abstraction allowed only when not ● high-level thinking loosing information – optimization ● memory-safe (performance) – array bounds checking, no pointer arithmeticJava course – IAG0040 Lecture 2Anton Keks Slide 2
  3. 3. Objects and Classes ● Classes introduce new types into a program ● First appeared in Simula-67 to define classes of objects ● In OOP, Objects are instances of Classes ● All objects of the same class share – same properties: ● fields hold internal state – same behavior, dependent on the state: ● methods used for communication with objectsJava course – IAG0040 Lecture 2Anton Keks Slide 3
  4. 4. References ● You manipulate objects with references – Like remote control of a TV is a reference to the TV ● Objects are stored on the heap, independently – String s; // is a reference to nothing – s = “hello”; // now s points to a newly created // String object on the heap ● You must create all the objects before using them – s = new Date(); ● There are many other built-in types (classes), like Integer, Date, Currency, BigDecimal, etc, and making your own types is the point of OOPJava course – IAG0040 Lecture 2Anton Keks Slide 4
  5. 5. Variables and Fields ● Local variables – inside of methods – final variables cant change their values ● Class members (fields) – inside of classes, outside of methods ● Global variables – dont exist in Java – can be simulated with static class members ● Constants are static final fieldsJava course – IAG0040 Lecture 2Anton Keks Slide 5
  6. 6. Primitive types ● Only primitives are not objects in Java – But, there are wrapper classes. Unlike in C, formats and sizes are standardized and are always the same. Primitive Size Min Max Wrapper Default boolean 1 bit? false true Boolean false char 16 bit Unicode 0 216-1 (UTF-16) Character u0000 byte 8 bit -128 127 Byte (byte)0 short 16 bit -215 215-1 Short (short)0 int 32 bit -231 231-1 Integer 0 long 64 bit -263 263-1 Long 0L float 32 bit IEEE 754 IEEE 754 Float 0.0f double 64 bit IEEE 754 IEEE 754 Double 0.0d void - - - Void -Java course – IAG0040 Lecture 2Anton Keks Slide 6
  7. 7. High-precision numbers ● BigInteger and BigDecimal – these are classes, not primitives – immutable – cannot be used with operators, operations are methods – slower, but long and precise ● BigInteger – arbitrary long integers ● BigDecimal – arbitrary precision fixed point numbersJava course – IAG0040 Lecture 2Anton Keks Slide 7
  8. 8. Fields (Members) ● A field (or member) is a class variable, holds its state ● Field names are in “lowerCamelCase” ● Constant names are in UPPER_CASE ● Initialized either automatically or manually ● class MyClass { boolean isEmpty; // a field int count; // a second field static String foo = “bar”; // a static field static final double E = Math.E; // a constant static { foo = “fooBar”; // static block } }Java course – IAG0040 Lecture 2Anton Keks Slide 8
  9. 9. Methods ● A method is a class function, used to execute an operation on a class (aka send a message) ● class MyClass { int count; // a field void doStuff(int count, short b) { // a method this.count = count * b; // method body } int getCount() { // second method, return count; // returning a value } } ● Methods can be overloaded ● Parameters are passed as references ● Local variables are enforced to be manually initializedJava course – IAG0040 Lecture 2Anton Keks Slide 9
  10. 10. Accessing fields and methods ● Java uses a dot . as an accessor operator – MyClass myObject = new MyClass(); myObject.isEmpty = true; // field access – myObject.setCount(5); // method call – int j = new MyClass().getCount(); // also allowed ● Static fields and methods are accessed through the class itself, not instance variables – int i = Integer.MAX_VALUE; ● “Deep” access is possible – System.out.println(“Hello!”);Java course – IAG0040 Lecture 2Anton Keks Slide 10
  11. 11. Access Modifiers and Visibility ● Several access modifiers are in use (keywords): – private – accessible only in the same class – default, no keyword (package local) – accessible only in the same package – protected – accessible in the same package and subclasses – public – accessible from anywhere ● public class HelloWorld { // public class int a; // package local field private int b; // private field protected void doStuff() {} // protected method private HelloWorld() {} // private constructor }Java course – IAG0040 Lecture 2Anton Keks Slide 11
  12. 12. Encapsulation ● Use the most restrictive access modifiers as possible ● Classes hide implementations and internal details from their users – This allows changing of internals any times without breaking any usages – The less internals exposed, the easier is to change them ● Public/accessible interface must be obvious to use; dont do anything unexpected!Java course – IAG0040 Lecture 2Anton Keks Slide 12
  13. 13. Operators ● Arithmetical: + - * / % ● Bitwise: ~ & | ^ << >> >>> ● Boolean: ! & | ^ ● Relational: < > <= >= == != ● Conditional: && || ^^ ternary: ? : ● Assignment: = += -= *= /= %= <<= >>= &= |= ^= ● String concatenation: + ● Unary: + - ++ -- ! ~ (cast) ● Type comparision: instanceofJava course – IAG0040 Lecture 2Anton Keks Slide 13
  14. 14. Naming convention ● Packages: lower case, reversed domain name as prefix – net.azib.hello, java.util, com.sun.internal ● Classes: camel case, should contain a noun – String, ArrayList, System, VeryAngryDogWithTeeth ● Interfaces: camel case, often adjectives – Collection, List, Comparable, Iterable ● Variables and fields: lower camel case, often include the class name – isEmpty, count, i, veryLongArrayList ● Methods: lower camel case, contain verbs – doStuff, processRequest, length, getLength ● Constants: underscore (_) separated upper case – Math.PI, MAX_VALUE, NAME_PREFIX ● Names should be obvious and descriptive!!! – System.currentTimeMillis(), s.substring(0, 3), s.toCharArray()Java course – IAG0040 Lecture 2Anton Keks Slide 14

×