×
Time can be your best friend and your worst enemy depending on whether you use it or waste it.
--Your friends at LectureNotes
Close

Core Java

by Mir Jasif
Type: NoteCourse: B.Tech Specialization: Computer Science EngineeringDownloads: 60Views: 779Uploaded: 2 months agoAdd to Favourite

Share it with your friends

Suggested Materials

Leave your Comments

Contributors

Mir Jasif
Mir Jasif
Java Java Notes for Professionals ® Notes for Professionals 900+ pages of professional hints and tricks GoalKicker.com Free Programming Books Disclaimer This is an unocial free book created for educational purposes and is not aliated with ocial Java® group(s) or company(s). All trademarks and registered trademarks are the property of their respective owners
Contents About ................................................................................................................................................................................... 1 Chapter 1: Getting started with Java Language .......................................................................................... 2 Section 1.1: Creating Your First Java Program ........................................................................................................... 2 Chapter 2: Type Conversion .................................................................................................................................... 8 Section 2.1: Numeric primitive casting ......................................................................................................................... 8 Section 2.2: Basic Numeric Promotion ........................................................................................................................ 8 Section 2.3: Non-numeric primitive casting ................................................................................................................ 8 Section 2.4: Object casting ........................................................................................................................................... 9 Section 2.5: Testing if an object can be cast using instanceof ................................................................................. 9 Chapter 3: Getters and Setters ........................................................................................................................... 10 Section 3.1: Using a setter or getter to implement a constraint ............................................................................. 10 Section 3.2: Why Use Getters and Setters? .............................................................................................................. 10 Section 3.3: Adding Getters and Setters ................................................................................................................... 11 Chapter 4: Reference Data Types .................................................................................................................... 13 Section 4.1: Dereferencing .......................................................................................................................................... 13 Section 4.2: Instantiating a reference type ............................................................................................................... 13 Chapter 5: Java Compiler - 'javac' .................................................................................................................... 14 Section 5.1: The 'javac' command - getting started ................................................................................................ 14 Section 5.2: Compiling for a dierent version of Java ............................................................................................ 16 Chapter 6: Documenting Java Code ................................................................................................................. 18 Section 6.1: Building Javadocs From the Command Line ....................................................................................... 18 Section 6.2: Class Documentation ............................................................................................................................. 18 Section 6.3: Method Documentation ......................................................................................................................... 19 Section 6.4: Package Documentation ....................................................................................................................... 20 Section 6.5: Links ......................................................................................................................................................... 20 Section 6.6: Code snippets inside documentation ................................................................................................... 21 Section 6.7: Field Documentation .............................................................................................................................. 22 Section 6.8: Inline Code Documentation ................................................................................................................... 22 Chapter 7: Command line Argument Processing ....................................................................................... 24 Section 7.1: Argument processing using GWT ToolBase ......................................................................................... 24 Section 7.2: Processing arguments by hand ............................................................................................................ 24 Chapter 8: The Java Command - 'java' and 'javaw' ................................................................................. 27 Section 8.1: Entry point classes .................................................................................................................................. 27 Section 8.2: Troubleshooting the 'java' command .................................................................................................. 27 Section 8.3: Running a Java application with library dependencies ..................................................................... 29 Section 8.4: Java Options ........................................................................................................................................... 30 Section 8.5: Spaces and other special characters in arguments ........................................................................... 31 Section 8.6: Running an executable JAR file ............................................................................................................ 33 Section 8.7: Running a Java applications via a "main" class ................................................................................. 33 Chapter 9: Date Class ............................................................................................................................................... 35 Section 9.1: Convert java.util.Date to java.sql.Date .................................................................................................. 35 Section 9.2: A basic date output ................................................................................................................................ 35 Section 9.3: Java 8 LocalDate and LocalDateTime objects ................................................................................... 36 Section 9.4: Creating a Specific Date ........................................................................................................................ 37 Section 9.5: Converting Date to a certain String format ......................................................................................... 37 Section 9.6: LocalTime ................................................................................................................................................ 38
Section 9.7: Convert formatted string representation of date to Date object ..................................................... 38 Section 9.8: Creating Date objects ............................................................................................................................ 39 Section 9.9: Comparing Date objects ........................................................................................................................ 39 Section 9.10: Converting String into Date ................................................................................................................. 42 Section 9.11: Time Zones and java.util.Date .............................................................................................................. 42 Chapter 10: Dates and Time (java.time.*) ...................................................................................................... 44 Section 10.1: Calculate Dierence between 2 LocalDates ....................................................................................... 44 Section 10.2: Date and time ........................................................................................................................................ 44 Section 10.3: Operations on dates and times ........................................................................................................... 44 Section 10.4: Instant ..................................................................................................................................................... 44 Section 10.5: Usage of various classes of Date Time API ....................................................................................... 45 Section 10.6: Date Time Formatting .......................................................................................................................... 47 Section 10.7: Simple Date Manipulations ................................................................................................................... 47 Chapter 11: LocalTime ............................................................................................................................................... 49 Section 11.1: Amount of time between two LocalTime ............................................................................................. 49 Section 11.2: Intro ......................................................................................................................................................... 50 Section 11.3: Time Modification ................................................................................................................................... 50 Section 11.4: Time Zones and their time dierence ................................................................................................. 50 Chapter 12: Literals .................................................................................................................................................... 52 Section 12.1: Using underscore to improve readability ............................................................................................ 52 Section 12.2: Hexadecimal, Octal and Binary literals ............................................................................................... 52 Section 12.3: Boolean literals ...................................................................................................................................... 53 Section 12.4: String literals .......................................................................................................................................... 53 Section 12.5: The Null literal ........................................................................................................................................ 54 Section 12.6: Escape sequences in literals ................................................................................................................ 54 Section 12.7: Character literals ................................................................................................................................... 55 Section 12.8: Decimal Integer literals ......................................................................................................................... 55 Section 12.9: Floating-point literals ............................................................................................................................ 56 Chapter 13: Operators .............................................................................................................................................. 59 Section 13.1: The Increment/Decrement Operators (++/--) .................................................................................... 59 Section 13.2: The Conditional Operator (? :) ............................................................................................................. 59 Section 13.3: The Bitwise and Logical Operators (~, &, |, ^) ..................................................................................... 61 Section 13.4: The String Concatenation Operator (+) .............................................................................................. 62 Section 13.5: The Arithmetic Operators (+, -, *, /, %) ................................................................................................ 64 Section 13.6: The Shift Operators (<<, >> and >>>) ................................................................................................... 66 Section 13.7: The Instanceof Operator ...................................................................................................................... 67 Section 13.8: The Assignment Operators (=, +=, -=, *=, /=, %=, <<=, >>= , >>>=, &=, |= and ^=) ................................. 68 Section 13.9: The conditional-and and conditional-or Operators ( && and || ) ..................................................... 70 Section 13.10: The Relational Operators (<, <=, >, >=) ............................................................................................... 71 Section 13.11: The Equality Operators (==, !=) ............................................................................................................. 72 Section 13.12: The Lambda operator ( -> ) ................................................................................................................ 74 Chapter 14: Primitive Data Types ...................................................................................................................... 75 Section 14.1: The char primitive .................................................................................................................................. 75 Section 14.2: Primitive Types Cheatsheet .................................................................................................................. 75 Section 14.3: The float primitive ................................................................................................................................. 76 Section 14.4: The int primitive ..................................................................................................................................... 77 Section 14.5: Converting Primitives ............................................................................................................................ 78 Section 14.6: Memory consumption of primitives vs. boxed primitives .................................................................. 78 Section 14.7: The double primitive ............................................................................................................................. 79 Section 14.8: The long primitive .................................................................................................................................. 80
Section 14.9: The boolean primitive ........................................................................................................................... 81 Section 14.10: The byte primitive ................................................................................................................................ 81 Section 14.11: Negative value representation ............................................................................................................ 82 Section 14.12: The short primitive ............................................................................................................................... 83 Chapter 15: Constructors ........................................................................................................................................ 84 Section 15.1: Default Constructor ............................................................................................................................... 84 Section 15.2: Call parent constructor ......................................................................................................................... 85 Section 15.3: Constructor with Arguments ................................................................................................................ 86 Chapter 16: Object Class Methods and Constructor ................................................................................. 88 Section 16.1: hashCode() method ............................................................................................................................... 88 Section 16.2: toString() method .................................................................................................................................. 90 Section 16.3: equals() method .................................................................................................................................... 91 Section 16.4: wait() and notify() methods ................................................................................................................. 93 Section 16.5: getClass() method ................................................................................................................................. 95 Section 16.6: clone() method ...................................................................................................................................... 96 Section 16.7: Object constructor ................................................................................................................................. 97 Section 16.8: finalize() method ................................................................................................................................... 98 Chapter 17: Annotations ........................................................................................................................................ 100 Section 17.1: The idea behind Annotations .............................................................................................................. 100 Section 17.2: Defining annotation types .................................................................................................................. 100 Section 17.3: Runtime annotation checks via reflection ........................................................................................ 102 Section 17.4: Built-in annotations ............................................................................................................................. 102 Section 17.5: Compile time processing using annotation processor .................................................................... 105 Section 17.6: Repeating Annotations ....................................................................................................................... 109 Section 17.7: Inherited Annotations .......................................................................................................................... 110 Section 17.8: Getting Annotation values at run-time ............................................................................................. 111 Section 17.9: Annotations for 'this' and receiver parameters ............................................................................... 112 Section 17.10: Add multiple annotation values ....................................................................................................... 113 Chapter 18: Immutable Class .............................................................................................................................. 114 Section 18.1: Example without mutable refs ............................................................................................................ 114 Section 18.2: What is the advantage of immutability? .......................................................................................... 114 Section 18.3: Rules to define immutable classes .................................................................................................... 114 Section 18.4: Example with mutable refs ................................................................................................................. 115 Chapter 19: Immutable Objects ......................................................................................................................... 116 Section 19.1: Creating an immutable version of a type using defensive copying .............................................. 116 Section 19.2: The recipe for an immutable class .................................................................................................... 116 Section 19.3: Typical design flaws which prevent a class from being immutable .............................................. 117 Chapter 20: Visibility (controlling access to members of a class) .................................................. 121 Section 20.1: Private Visibility ................................................................................................................................... 121 Section 20.2: Public Visibility .................................................................................................................................... 121 Section 20.3: Package Visibility ............................................................................................................................... 122 Section 20.4: Protected Visibility .............................................................................................................................. 122 Section 20.5: Summary of Class Member Access Modifiers ................................................................................. 123 Section 20.6: Interface members ............................................................................................................................ 123 Chapter 21: Generics ................................................................................................................................................ 124 Section 21.1: Creating a Generic Class ..................................................................................................................... 124 Section 21.2: Deciding between `T`, `? super T`, and `? extends T` ........................................................................ 126 Section 21.3: The Diamond ....................................................................................................................................... 128 Section 21.4: Declaring a Generic Method .............................................................................................................. 128

Lecture Notes