Java Course 2: Basics
Upcoming SlideShare
Loading in...5
×
 

Java Course 2: Basics

on

  • 2,758 views

Lecture 2 from the IAG0040 Java course in TTÜ.

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

Statistics

Views

Total Views
2,758
Views on SlideShare
1,261
Embed Views
1,497

Actions

Likes
2
Downloads
45
Comments
0

2 Embeds 1,497

http://kurapov.name 1491
http://localhost 6

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Java Course 2: Basics Java Course 2: Basics Presentation Transcript

  • Java course - IAG0040 Java Basics, Program FlowAnton Keks 2011
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • Operators ● Arithmetical: + - * / % ● Bitwise: ~ & | ^ << >> >>> ● Boolean: ! & | ^ ● Relational: < > <= >= == != ● Conditional: && || ^^ ternary: ? : ● Assignment: = += -= *= /= %= <<= >>= &= |= ^= ● String concatenation: + ● Unary: + - ++ -- ! ~ (cast) ● Type comparision: instanceofJava course – IAG0040 Lecture 2Anton Keks Slide 13
  • 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