You are on page 1of 10

Fundamentals of the Java

Programming Language,
Java SE 6
Student Guide

SL-110-SE6 E.1 Rev E.1

D61796GC11
Edition 1.1
June 2010
D67896

Copyright 2010, Oracle and/or its affiliates. All rights reserved.


Disclaimer
This document contains proprietary information, is provided under a license agreement containing restrictions on use and
disclosure, and is protected by copyright and other intellectual property laws. You may copy and print this document solely for
your own use in an Oracle training course. The document may not be modified or altered in any way. Except as expressly
permitted in your license agreement or allowed by law, you may not use, share, download, upload, copy, print, display,
perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express
authorization of Oracle.
The information contained in this document is subject to change without notice. If you find any problems in the document,
please report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This
document is not warranted to be error-free.
Sun Microsystems, Inc. Disclaimer
This training manual may include references to materials, offerings, or products that were previously offered by Sun
Microsystems, Inc. Certain materials, offerings, services, or products may no longer be offered or provided. Oracle and its
affiliates cannot be held responsible for any such references should they appear in the text provided.
Restricted Rights Notice
If this documentation is delivered to the U.S. Government or anyone using the documentation on behalf of the U.S.
Government, the following notice is applicable:
U.S. GOVERNMENT RIGHTS
The U.S. Governments rights to use, modify, reproduce, release, perform, display, or disclose these training materials are
restricted by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract.
Trademark Notice
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners.

This page intentionally left blank.

This page intentionally left blank.

Table of Contents
About This Course ...............................................................Preface-xi
Course Goals.......................................................................... Preface-xi
Course Map............................................................................Preface-xii
Topics Not Covered.............................................................Preface-xiii
How Prepared Are You?.....................................................Preface-xiv
Introductions ......................................................................... Preface-xv
How to Use Course Materials ............................................Preface-xvi
Conventions .........................................................................Preface-xvii
Icons .............................................................................Preface-xvii
Typographical Conventions ................................... Preface-xviii
Additional Conventions............................................. Preface-xix
Explaining Java Technology........................................................1-1
Objectives ........................................................................................... 1-1
Progress Check ................................................................................... 1-2
Relevance............................................................................................. 1-4
Additional Resources ........................................................................ 1-5
Key Concepts of the Java Programming Language ...................... 1-6
Object-Oriented ......................................................................... 1-7
Distributed ................................................................................. 1-8
Simple ......................................................................................... 1-9
Multithreaded.......................................................................... 1-10
Secure........................................................................................ 1-10
Java Technology Product Groups.................................................. 1-17
Identifying Java Technology Product Groups .................... 1-17
Choosing the Correct Java Technology Product Group.... 1-18
Using the Java Platform, Standard Edition SDK
Components.......................................................................... 1-19
Product Life Cycle (PLC) Stages .................................................... 1-22
Analysis Stage.......................................................................... 1-23
Design Stage............................................................................. 1-24
Development Stage ................................................................. 1-25
Testing Stage............................................................................ 1-26
Implementation Stage............................................................. 1-27

v
Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.Sun Services, Revision E.1

Maintenance Stage ................................................................. 1-28


End-of-Life (EOL) Stage ......................................................... 1-29
Why Should You Follow the PLC?....................................... 1-30
Analyzing a Problem and Designing a Solution ........................... 2-1
Objectives ........................................................................................... 2-1
Progress Check ................................................................................... 2-2
Relevance............................................................................................. 2-3
Additional Resources ........................................................................ 2-4
Analyzing a Problem Using Object-Oriented Analysis................ 2-5
Identifying a Problem Domain ............................................... 2-7
Identifying Objects................................................................... 2-8
Additional Criteria for Recognizing Objects...................... 2-10
Identifying Object Attributes and Operations .................... 2-12
Case Study Solution................................................................ 2-15
Designing Classes ............................................................................ 2-17
Modeling Classes .................................................................... 2-20
Developing and Testing a Java Technology Program ................. 3-1
Objectives ........................................................................................... 3-1
Progress Check ................................................................................... 3-2
Relevance............................................................................................. 3-4
Additional Resources ........................................................................ 3-5
Identifying the Components of a Class........................................... 3-6
Structuring Classes .................................................................. 3-7
Class Declaration....................................................................... 3-9
Variable Declarations and Assignments.............................. 3-10
Comments ................................................................................ 3-11
Methods.................................................................................... 3-12
Creating and Using a Test Class .................................................... 3-14
The main Method ................................................................... 3-15
Compiling and Executing (Testing) a Program........................... 3-17
Compiling a Program ............................................................. 3-17
Executing (Testing) a Program.............................................. 3-18
Declaring, Initializing, and Using Variables................................... 4-1
Objectives ........................................................................................... 4-1
Progress Check ................................................................................... 4-2
Relevance............................................................................................. 4-4
Additional Resources ........................................................................ 4-5
Identifying Variable Use and Syntax .............................................. 4-6
Uses for Variables ..................................................................... 4-7
Variable Declaration and Initialization................................. 4-8
Describing Primitive Data Types................................................... 4-10
Integral Primitive Types ........................................................ 4-10
Floating Point Primitive Types ............................................. 4-12
Textual Primitive Type.......................................................... 4-13

vi

Fundamentals of the Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision E.1

Logical Primitive Type ........................................................... 4-14


Choosing a Data Type ............................................................ 4-14
Declaring Variables and Assigning Values to Variables............ 4-15
Naming a Variable .................................................................. 4-15
Assigning a Value to a Variable............................................ 4-17
Constants.................................................................................. 4-19
Storing Primitives and Constants in Memory .................... 4-21
Using Arithmetic Operators to Modify Values ........................... 4-22
Standard Mathematical Operators ....................................... 4-22
Increment and Decrement Operators (++ and --) ............. 4-23
Operator Precedence .............................................................. 4-24
Using Promotion and Type Casting.............................................. 4-26
Promotion................................................................................ 4-27
Type Casting ............................................................................ 4-28
Compiler Assumptions for Integral and Floating Point Data
Types...................................................................................... 4-29
Creating and Using Objects ............................................................5-1
Objectives ........................................................................................... 5-1
Progress Check ................................................................................... 5-2
Relevance............................................................................................. 5-4
Additional Resources ........................................................................ 5-5
Declaring Object References, Instantiating Objects, and Initializing
Object References ............................................................................ 5-6
Declaring Object Reference Variables .................................... 5-8
Instantiating an Object ............................................................. 5-8
Initializing Object Reference Variables.................................. 5-9
Using an Object Reference Variable to Manipulate Data. 5-10
Storing Object Reference Variables in Memory.................. 5-11
Assigning a Reference From One Variable to Another ..... 5-12
Using the String Class................................................................... 5-14
Creating a String Object With the new Keyword ............. 5-14
Creating a String Object Without the new Keyword ....... 5-14
Storing String Objects in Memory...................................... 5-15
Using Reference Variables for String Objects................... 5-16
Investigating the Java Class Libraries ........................................... 5-17
Java Class Library Specification............................................ 5-17
Using the Java Class Library Specification to Learn About a
Method................................................................................... 5-19
Using Operators and Decision Constructs ....................................6-1
Objectives ........................................................................................... 6-1
Progress Check ................................................................................... 6-2
Relevance............................................................................................. 6-3
Additional Resources ........................................................................ 6-4
Using Relational and Conditional Operators ................................ 6-5
Elevator Example ...................................................................... 6-6

vii
Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision E.1

Relational Operators................................................................ 6-8


Testing Equality Between Strings ........................................... 6-9
Conditional Operators........................................................... 6-10
Creating if and if/else Constructs ........................................... 6-11
The if Construct ..................................................................... 6-11
Nested if Statements ............................................................ 6-14
The if/else Construct .......................................................... 6-15
Chaining if/else Constructs.............................................. 6-18
Using the switch Construct........................................................... 6-20
When to Use switch Constructs .......................................... 6-22
Using Loop Constructs ................................................................... 7-1
Objectives ........................................................................................... 7-1
Progress Check ................................................................................... 7-2
Relevance............................................................................................. 7-3
Creating while Loops ....................................................................... 7-4
Nested while Loops ................................................................. 7-6
Developing a for Loop ..................................................................... 7-9
Nested for Loops ................................................................... 7-12
Coding a do/while Loop................................................................ 7-13
Nested do/while Loops ........................................................ 7-16
Comparing Loop Constructs ................................................. 7-17
Developing and Using Methods ..................................................... 8-1
Objectives ........................................................................................... 8-1
Progress Check ................................................................................... 8-2
Relevance............................................................................................. 8-4
Creating and Invoking Methods...................................................... 8-5
Basic Form of a Method ........................................................... 8-6
Invoking a Method From a Different Class........................... 8-7
Invoking a Method in the Same Class.................................... 8-8
Guidelines for Invoking Methods ........................................ 8-10
Passing Arguments and Returning Values .................................. 8-12
Declaring Methods With Arguments................................... 8-13
Invoking Methods With Arguments.................................... 8-14
Declaring Methods With Return Values............................. 8-16
Returning a Value ................................................................... 8-16
Receiving Return Values....................................................... 8-17
Advantages of Method Use ................................................... 8-18
Creating static Methods and Variables..................................... 8-19
Declaring static Methods ................................................... 8-19
Invoking static Methods..................................................... 8-19
Declaring static Variables .................................................. 8-21
Accessing static Variables .................................................. 8-21
Static Methods and Variables in the Java API ................... 8-22
When to Declare a static Method or Variable ................. 8-23
Using Method Overloading............................................................ 8-25

viii

Fundamentals of the Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision E.1

Method Overloading and the Java API .............................. 8-27


Uses for Method Overloading............................................... 8-28
Implementing Encapsulation and Constructors............................9-1
Objectives ........................................................................................... 9-1
Progress Check ................................................................................... 9-2
Relevance............................................................................................. 9-3
Using Encapsulation.......................................................................... 9-4
Visibility Modifiers ................................................................... 9-4
The public Modifier ................................................................ 9-5
The private Modifier.............................................................. 9-7
Interface and Implementation............................................... 9-10
Get and Set Methods .............................................................. 9-11
Encapsulated Elevator............................................................ 9-16
Describing Variable Scope .............................................................. 9-21
How Instance Variables and Local Variables Appear in
Memory ................................................................................. 9-22
Creating Constructors ..................................................................... 9-24
Default Constructor ................................................................ 9-26
.Overloading Constructors .................................................... 9-28
Creating and Using Arrays ............................................................10-1
Objectives ......................................................................................... 10-1
Progress Check ................................................................................. 10-2
Relevance........................................................................................... 10-4
Creating One-Dimensional Arrays................................................ 10-5
Declaring a One-Dimensional Array ................................... 10-6
Instantiating a One-Dimensional Array .............................. 10-7
Initializing a One-Dimensional Array ................................. 10-8
Declaring, Instantiating, and Initializing One-Dimensional
Arrays .................................................................................... 10-9
Accessing a Value Within an Array .................................. 10-10
Storing One-Dimensional Arrays in Memory ................. 10-11
Setting Array Values Using the length Attribute and a Loop10-14
The length Attribute ........................................................... 10-14
Setting Array Values Using a Loop.................................... 10-14
Using the args Array in the main Method ................................ 10-16
Converting String Arguments to Other Types............... 10-17
The varargs Feature ............................................................ 10-17
Describing Two-Dimensional Arrays ......................................... 10-18
Declaring a Two-Dimensional Array................................. 10-19
Instantiating a Two-Dimensional Array............................ 10-19
Initializing a Two-Dimensional Array............................... 10-20
Implementing Inheritance ..............................................................11-1
Objectives ......................................................................................... 11-1
Progress Report ................................................................................ 11-2

ix
Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision E.1

Relevance........................................................................................... 11-4
Inheritance......................................................................................... 11-5
Superclasses and Subclasses.................................................. 11-6
Testing Superclass and Subclass Relationships.................. 11-8
Modeling Superclasses and Subclasses ............................. 11-10
Declaring a Superclass.......................................................... 11-11
Declaring a Subclass ............................................................. 11-13
Example of Declaring a Subclass ........................................ 11-14
Abstraction...................................................................................... 11-15
Abstraction in Your Analysis and Design ......................... 11-15
Classes in the Java API .................................................................. 11-17
Implicitly Available Classes ................................................ 11-17
Importing and Qualifying Classes...................................... 11-18
Where Do You Go From Here? .......................................................A-1
Getting Ready to Program ............................................................... A-2
Downloading Java Technology.............................................. A-2
Downloading the Java SE API Specification........................ A-2
Setting Up Your Computer to Develop and Execute Java
Technology Programs........................................................... A-3
Downloading a Development Environment or Debugger A-3
References .......................................................................................... A-4
Basic Java Technology ............................................................. A-4
Applets...................................................................................... A-5
Online Tutorial ......................................................................... A-5
Technical Articles, Tips, and White Papers.......................... A-5
Java Programming Language Keywords ......................................B-1
Keywords ............................................................................................B-2
Java Programming Language Naming Conventions....................C-1
Class, Method, and Variable Identifiers ........................................ C-2
Navigating the Solaris Operating System..................................D-1
Solaris OS Quick Reference ............................................................. D-2

Fundamentals of the Java Programming Language


Copyright 2007 Sun Microsystems, Inc. All Rights Reserved. Sun Services, Revision E.1

You might also like