Chapter 1 Notes - Briar Cliff University

moodusroundoSoftware and s/w Development

Aug 15, 2012 (4 years and 8 months ago)

205 views

Day 1: Intro to Java

Getting Java

Go to
http://www.oracle.com/technetwork/java/javase/downloads/jdk
-
netbeans
-
jsp
-
142931.html
. Accept the license agreement

and then download the software.


Run the setup program. It should install two things on your computer:

1.

The Java Development Kit (JDK). This is Java.

2.

The NetBeans Integrated Development Environment (IDE). This is your editor and
compiler.

Creating a New
Java Application

1.

Create a folder called
Java

on your M drive for storing all of your programs this
semester.

2.

Start NetBeans.

3.

Choose
File | New Project
. Under
Categories
, select
General
. Under
Projects
, select
Java Application

and click
Next
.

4.

Under
Project

Name
, enter the name of your app. Make sure the Project Location is
set to your M drive. If NetBeans has problems with the M drive, you can always save
to your desktop or a USB drive.

5.

Ensure that the
Create Main Class

checkbox is checked.

6.

Click
Finish
. Yo
ur project is displayed in the
Project

window and
Main.java

opens in
the Source Editor.

Changing some defaults

All options are found by clicking on
Tools

|
Options
.

Indentation



Click on the
Editor

button.



Under
Languages
, click on
Java
.



Under
Category
, click on
Tabs and indents
.



Set
Number of spaces per indent

and
Tab Size

to 3.

Braces



Under
Category
, click on
Braces
.



Under
Braces Placement
, select
New Line

for all three options.



Under
Braces Generation
, select
Generate

for all four options.

Change
comment color



Click on the
Fonts and Colors

button on the top.



Click on the
Syntax

tab.



Under
Category
, click on
Comments
.



Under
Foreground
, click on the dropdown and select a dark green color.



Java

Java is an
object
-
oriented
programming language. Almost

everything in Java is an object.
The only data elements in Java that are NOT objects are the basic data types (called
primitives

in Java). Java’s primitives are: integers, floating
-
point numbers, characters, and
Booleans.
Everything else

(including String
s and arrays) is an object.

Java is a relatively young programming language; it has been around since 1995. The best
way to learn how to write programs in a language is to read programs written in the
language.

Sample program

public class Sample001 {




public static void main (String args[])


{


int i = 100;


double x = 1.5;


boolean flag = true;


char middleInitial = 'X';


String firstName = "George";


String lastName = "Washington";



System.out.println (" Integer is: " + i );


System.out.println (" Double is: " + x );


System.out.println (" Flag is: " + flag);


System.out.println ("Full name is: " + firstName +


" " + middleInitial + ". " +
lastName);


}

}

This program demonstrates:



Basic structure of a Java program. Every program we write will be a class with a
public static void main

procedure.



Every statement ends with a semicolon.



The beginning of a method (procedure/function) body is

marked with a left curly
bracket ({) and the end is marked with a right curly bracket (}).



Basic types we will be using in this class:



int

(also byte, short, and long)



double

(also float)



boolean

(can be assigned
true

or
false
).



char

We won't be using it
very much. Note that single quotes are used to
enclose a
char

constant. Also note that characters in Java are
represented using
Unicode

(2 bytes per character).



String

Note that "String" is capitalized and that it is NOT blue and bold
like the other data t
ypes! This is because it is not a "primitive" (built
-
in) data type; it is a class/object. String constants are enclosed in
double quotation marks. Java is
case
-
sensitive!!!!!

"String" is not the
same as "string"!



Variable declaration and initialization.



F
ind out what happens when you don't initialize a variable. It used to be that
numbers are initialized to 0, Boolean to false, char to null character, and string to
empty string. Now, you get compile errors.



Output to the screen

To

output to the screen, use
System.out.print

or
System.out.println
. The argument
(only one argument is allowed) to the print/println statements must be a string. You
can concatenate strings using the "+" sign. Numbers are automatically converted to
strings
when used in a print or println statement.

2.3 Identifiers

Identifiers are used to name variables, constants, procedures and functions (called
methods

in Java), classes, etc. All identifiers must follow the following rules:



Must begin with a letter or an
underscore (use a letter)



Must be composed of letters, digits, underscores, and dollar signs (don't use dollar
signs


they are reserved for system use).



Cannot be a keyword, a boolean literal, or the reserved word
null
.



Are
case
-
sensitive!



It must be uni
que within its scope. A variable may have the same name as a variable
whose declaration appears in a different scope. In some situations, a variable may
share the same name as another variable if it is declared within a nested block of
code. (We will cover

this in the next section, Scope.)

Naming Conventions

Variable names: begin with a lowercase letter. If a variable name consists of more than one
word, the words are joined together, and each word after the first begins with an uppercase
letter, like this
:

isVisible
.

Constant names: all upper
-
case, with underscores used to separate multi
-
word identifiers.

Class names: begin with an uppercase letter.

The underscore character (
_
) is acceptable anywhere in a name, but by convention is used
only to separate w
ords in constants (because constants are all caps by convention and thus
cannot be case
-
delimited).



2.4 Variables

Variables in Java may be either
primitive

or
reference
.

A
primitive variable

actually holds its data in the memory location that the variabl
e refers
to. Primitive data types are:



Integer types:
byte
(1)
, short

(2)
, int

(4)
, long

(8 bytes)



Floating point types:
float

(4 bytes)
, double

(8 bytes)



Character:
char

(implemented as 16
-
bit Unicode)



Boolean:
boolean

(can only take on the values
true

and
false
)

Keyword

Description

Size/Format

(integers)

byte

Byte
-
length integer

8
-
bit two's complement

short

Short integer

16
-
bit two's complement

int

Integer

32
-
bit two's complement

long

Long integer

64
-
bit two's complement

(real numbers)

float

Single
-
precision floating point

32
-
bit IEEE 754

double

Double
-
precision floating point

64
-
bit IEEE 754

(other types)

char

A single character

16
-
bit Unicode character

boolean

A boolean value (
true

or
false
)

true or false


In this class, we will always use
int

when we need an integer type (unless we need a value
larger than 2 billion) and
double

when we need a floating
-
point type.

A
reference variable

holds the address of the data that it refers to.
All objects are
reference

variables
.



The
String

type is an object in Java, and therefore all strings are reference
variables.



Variable Initialization

Local variables and member variables can be initialized with an assignment statement when
they're declared. The data type of the

variable must match the data type of the value
assigned to it. Here are some examples, with the initialization code set in
red
:

// integers

byte largestByte
= Byte.MAX_VALUE
;

short largestShort
= Short.MAX_VALUE
;

int largestInteger
= Integer.MAX_VALUE
;

long largestLong
= Long.MAX_VALUE
;


// real numbers

float largestFloat
= Float.MAX_VALUE
;

double largestDouble
= Double.MAX_VALUE
;


// other primitive types

char aChar
= 'S'
;

boolean aBoolean
= true
;

Parameters and exception
-
handler parameters cannot be in
itialized in this way. The
value for a parameter is set by the caller.