Intro To Java

nutmegactΛογισμικό & κατασκευή λογ/κού

10 Νοε 2012 (πριν από 5 χρόνια και 8 μέρες)

185 εμφανίσεις

Intro to Java

With content from

Horstmann's

Java for Everyone

Goals


To learn


About computers and programming


How to compile and run your first Java program


How to recognize and correct simple compile
-
time
and run
-
time errors


How to describe an algorithm with pseudocode


Programs


A program is a sequence of instructions


Programs also usually include


Decisions


Data


Computers execute very basic instructions in
rapid succession


Programming is the act of designing and
implementing computer programs

Computer Hardware


Central Processing Unit (CPU)


Performs calculations


Memory (RAM)


Stores things CPU can directly work with


Secondary Storage


Stores things for later


Includes hard drive, flash drive, DVD and more


We'll focus on computer hardware at the end
of the semester

Running A Program


Programs are stored somewhere (hard disk)


When a program is started, it is brought into
memory, where the CPU can use it


The CPU runs (executes) the program one
instruction at a time


The CPU reads data, modifies it, and writes it
back to memory or other locations (screen)


Java Background


In 1991, James Gosling of Sun Microsystems
designed what would become the Java
programming language


Originally designed for consumer devices


Used in many cell phones


Gained popularity via Internet applets


Not the same thing as Javascript


Java Overview


Programming language


Syntax


Features


Safe


Simple


Platform
-
independent


Object oriented

The Java SDK & API


You need to install the Java SDK (Software
Development Kit) to create Java programs


Programming software


Application Programming Interface (API)


The API is a huge collection of software
packages that programmers can use


Makes programming a little easier


Java Platform


Java programs are instructions for a ‘virtual
machine’, making them platform
-
independent


The JVM + Java API are sometimes called the
Java Platform


Your programs run on top of the Java platform


IDE


IDE: Integrated Development Environment


While not strictly required, an IDE make
programming easier


Components of an IDE include:


Source code editor


Output window


Debugger


We'll be using Netbeans as our IDE


Netbeans

Source code files

Source code editor

Output window

Absolute Minimum

class HelloPrinter

{


public static void main(String[] args)


{



}

}

Analyzing HelloPrinter

Calling Java Library Methods


Line 5 shows how to 'call' a 'method' from the
Java API: System.out.println


Code that somebody else wrote for you!


Notice the dots (periods)


Parenthesis surround the arguments that you
'pass' to a method


We are passing a String "Hello World"

Hello


Getting to know
println


The
println

method prints a string or a
number and then starts a new line.

Java for Everyone by Cay
Horstmann

Copyright © 2009 by John Wiley & Sons. All rights reserved.

System.out.
println
(“Hello”);

System.out.
println
(“World!”);




Hello

World!



println

has a ‘cousin’
named
print

that
does not print a new line.

System.out.
print
(“00”);

System.out.
println
(3+4);




00


007


Making New Applications


Every application has the same basic layout


Some class


Main method


Statements


Initially we simply add our own ‘code’ inside
the main method


Later we'll need additional techniques to
handle more complex programs


Code


Code refers to your program or portion
thereof


Sometime called source code


Governed by rules and conventions


Statement


syntactic unit of a program

Code → Program


Your 'source code' is stored in .java files


The compiler generates .class files of 'byte code'


The byte code contains the program instructions in a
format the JVM can understand


While it's theoretically possible to edit byte code
directly, it is rarely done in practice

Organize Your Work


Use one folder per program


Netbeans does this by default


Can be many .java files per program / project


Know where your IDE stores your files!


Netbeans folder: src


Backup your work!


Use flash drives, the network drive or both


Rules and Conventions


Rules


Defined by language
syntax


Strict


Enforced by the compiler


Violations result in
syntax errors



Conventions


Less strict


Enforced by people


Violations may lead to:


Syntax errors


Run
-
time errors


Poor design


Importance of Naming


When naming things in Java use


CamelCase


Sensible identifiers


Note: some things start with a capital, some
do not


Good naming is a convention not a rule


Formatting


Java is case sensitive


Generally, whitespace is ignored by the
compiler


Vertically aligned code is easier to read

Strings


A strings is a sequence of characters


Must be enclosed in quotation marks


“this is a valid string”


“So is this list: 1,2,3,4!”


“This is not a valid string (unclosed)


Neither is this”


Comments


Comments are


Used to document code, leave notes or
explanations


Ignored by the compiler, but may be used by other
utilities like
Javadoc


Used by humans to understand code


// single line comment


/*
muliple

line comment */

Errors


Types


Syntax errors


Logic errors


Programming errors are also called bugs


A debugger is software that help programmers
locate & remove bugs

Syntax Errors


Compile time errors


Must be fixed before the program can run


Compiler flags as many errors as possible


One error may hide or generate other errors


Logic Errors


Run time errors


Program does not operate as intended


Cannot be detected by the compiler


Can be found by testing


Typically, testing cannot prove complete
correctness


Common Syntax Errors


Unbalanced


parens ( )


braces { } [ ]


quotes “ “


multi
-
line comment blocks /* */


Forgetting to end statements with a semicolon

Common Syntax Errors


Capitalization


Java is case sensitive


Misspellings


File & class names must match


Missing keywords

Typical


Development


Cycle

Begin

Edit
Program

Compile
Program

Compiler
Errors?

Test
Program

Run
-
time
Errors?

End

true

true

false

false

Outside the Clusters


Download and install the JDK


Download & install one or more IDE


BlueJ


Netbeans


Eclipse


Technically, you don't
have

to use an IDE


Determine where to store your files