Overview of the Java Programming Language

cabbagewheatInternet και Εφαρμογές Web

13 Νοε 2013 (πριν από 3 χρόνια και 8 μήνες)

76 εμφανίσεις

Overview of the Java Programming
Language


(2011 edition)

If you know C

you already know

the basics of Java

Common Programming Language Features


Comments


Data Types


Variable Declarations


Expressions


Flow of Control Statements


Functions and Subroutines


Macros and Preprocessor Commands


I/O Statements


Libraries


Compiler Directives


External Tools (Compiler, debugger etc)




Comments in Java


The ability to comment is the most
important feature in any programming
language!!!



Comments should precede any block
of code or any code that might be
difficult to understand. A comment
should describe the intent of what you
are trying to do.



Write your comments BEFORE you
write your code. Do not rely on the
code itself to document what you are
doing as the code may be incorrect



Comments may also be used to
temporarily remove a block of code
from your program



Special comments used to generate
help files



Special comments used in NetBeans
to keep track of unfinished tasks and
problems




/* This is a comment


it can span many lines */


//Single line comments


//* Javadoc comments */


//TODO


Hello World in Java

class Hello

{


public static void main(String[] args)

{


System.out.println(“Welcome to Java”);


}


}

6

Why Java?


Java enables users to develop and deploy
applications on the Internet on multiple platforms:
servers, desktop computers, hand
-
held and
embedded devices such as dvd and blue
-
ray players,
cell phones, RFID devices etc, regardless of the
underlying processor or operating system.



Java is a general purpose programming language.


Java is the Internet programming language.


Write once, deploy everywhere

7

Characteristics of Java


Java Is Simple



Java Is Object
-
Oriented



Java Is Distributed



Java Is Interpreted



Java Is Robust



Java Is Secure



Java Is Architecture
-
Neutral



Java Is Portable



Java's Performance



Java Is Multithreaded



Java Is Dynamic




www.cs.armstrong.edu/liang/intro8e/JavaCharacteristics.pdf

8

JDK Versions


JDK 1.02 (1995)


JDK 1.1 (1996)


JDK 1.2 (1998)


JDK 1.3 (2000)


JDK 1.4 (2002)


JDK 1.5 (2004) a. k. a. JDK 5 or Java 5


JDK 1.6 (2006) a. k. a. JDK 6 or Java 6


JDK 1.7 (possibly 2010) a. k. a. JDK 7 or
Java 7



9

JDK Editions


Java Standard Edition (J2SE)


J2SE can be used to develop client
-
side standalone
applications or applets.


Java Enterprise Edition (J2EE)


J2EE can be used to develop server
-
side applications such as
Java servlets and Java ServerPages.


Java Micro Edition (J2ME).


J2ME can be used to develop applications for mobile devices
such as cell phones.

The Course Text uses J2SE to introduce Java
programming.


10

Compiling Java Source Code

Unlike compiled programs written in languages such as C, C++
or Assembler, Java was designed to run object programs on any
platform. With Java, you write the program once, and compile
the source program into a special type of object code, known as
bytecode
. The bytecode can then run on any computer with a
Java Virtual Machine, as shown below. Java Virtual Machine is a
software that interprets Java bytecode.


Java Bytecode

Java Virtual
Machine

Any
Computer

Looking at Java as a Language


Remember


the syntax is like

Primitive Data Types


boolean, char,
byte, int, short,
long, float and
double.


Ordinal Constants in Java


char : ‘A’



int:
-
200,


1000000L


(unsigned) 127



0x80FA (hex),


007 (octal)



\
u0811’ (unicode)



range of values depends
on word size of the
machine. Usually 4 bytes
for an int.

“Ordinal” means “countable”




there is usually a next and previous value.

boolean data types

boolean found=false;

String password=“Swordfish”;


found=(myInput.equals(“password”);

if(found==true)


System.out.println(“Yes we found it!”)

else

System.out.println(“No we did not”);

Special Character Constants



\
n’
-

newline



\
r’
-

carriage return



\
t’


-

tab



\
\

-

backslash



\
’’
-

quoted apostrophe



\
”’

quoted quote



\
0’


null character



\
a’


audible alert



\
b’
-

backspace

Non
-
Ordinal Constants


float : 12.5E
-
12.
-
0.5


double: 12.5E+200


always use doubles




17

Numerical Data Types


Name




Range


Storage Size


byte


2
7
(
-
128) to 2
7

1 (127)

8
-
bit signed


short


2
15
(
-
32768) to 2
15

1 (32767)

16
-
bit signed


int


2
31
(
-
2147483648) to 2
31

1 (2147483647)

32
-
bit signed


long


2
63
to 2
63

1

64
-
bit signed


(i.e.,
-
922337203685
4775808


to 9223372036854775807)


float

Negative range:

32
-
bit IEEE 754



-
3.4028235E+38 to
-
1.4E
-
45


Positive range:


1.4E
-
45 to 3.4028235E+38


double

Negative range:

64
-
bit IEEE 754



-
1.7976931348623157E+308 to


-
4.9E
-
324


Pos
itive range:

4.9E
-
324 to 1.7976931348623157E+308


Non
-
Primitive Data Types

contain multiple values


arrays


classes (start with
the idea of C’s
structs)


Collections

Declaring Scalar Variables


short cake;


int i;


unsigned int value;


long face;


float icecream;


double mint;


boolean result;

Arrays

int [ ] x = new int[20];


x[5] = 7;


n=40;

String [ ] names;

names=new String[n];


//2 Dimensional Arrays

Vehicle[ ][ ] parkingLot=new Vehicle[40][25];



Operators in Java


Assignment: =



Arithmetic : * / +
-

++
--

%



Logical: && || !



Relational: > < == <>



Bitwise: & | ^ << >> ~



String: +



Grouping: () [] ,



Triadic: (cond) ? value1 : value2



Operators in C


Operators can be binary or
unary.



?: is a special triadic operator.


Any binary operator can be
combined with the assignment

operator, ie:



a*=3;



is the same as



a=a*3;



Assignment isn’t special


its
an operator like any other


a=b+(c=7);


Expressions: Operator Precedence


comma: sequence operator

,

13

Arithmetic Assignment

Bitwise assignment

= +=
-
= *= /= %=

&= |= ^= <<== >>=

12

logical and, logical or (short circuit operators

&& ||

11

bitwise or

|

10

bitwise xor

^

9

bitwise and

&

8

equal to, not equal to


= = !=

7

less than, less than or equal to

greater than, greater than or equal 2

<


<=

>


>=

6

bitwise left shift, bitwise right shift

<<


>>

5


plus, minus

+


-

4

times/divide/mod

*


/


%

3

Unary pre/post increment/ decrement

Unary plus/minus

Unary logical negation/bitwise complement

Unary cast (change
type
)

Dereference

Address of

size in bytes

++


--

+


-

!


~

(
type
)




2

Parentheses (grouping)

Brackets (array subscript)

Member selection

Member selection via pointer

()

[ ]

.


1

Description

Operator


Priority

Expressions: Operator Precedence


When in doubt use
brackets ()



If you know the correct
operator precedence, but
aren’t so sure others will
know it as well


use use
brackets ()



use brackets to make
your meaning clear ( :
-

)



did I mention you should
use () ?

Expressions: Type Precedence


float + double


double



float + int


float



short * long


long



char + int


int



address + int


address


When 2 operands are of

different types, the result is the

larger or more complicated type


When in doubt of the result, use the

cast operator



result = (float) (myDouble + myInt);


Expressions: Type Precedence


Remember:



x = 1/4; /* x


0 */



x = 1.0/7; /* x

.25*/


C does very little type

checking!!

Flow of Control: IF stmt


if(condition) stmt;


else stmt;


if(x>10) System.out.print(“Too Big”);


else System.out.print(“Value OK”);

Flow of Control: if stmt

if(x>10 && x<20)


{


/* block of code */


}

else


{


/* another block of


code */


}

Secret Slide

The triadic operator is a


shorter form of if/then/else


examples:



if(x>10) y=2; else y=0;


y= (x>10) ? 2 : 0



if(x>10) System.out.println(”X is big”);


else System.out.println(”X is small”);



System.out.println(x>10 ? “X is big” : “x is
small”);



if(a>b) return a; else return b;


return (a>b) ? a : b;

Flow of Control: switch/case

switch(ordinalValue)

{


case ‘A’:


case ‘a’:


puts(“A chosen”);


break;



case 2:


puts(“# 2 choice”);


break;



default:


puts(“None of the above”);

}

Flow of Control: while



while(condition)


{


int localValue;


doStuff();


}


Variables can be
declared at the start
of any block of code
{ }

Forever loop: while

while(true)


{


int localValue;


doStuff();


}

Flow of Control: do while


do

{


doStuff();


} while(condition);



This kind of loop is
always executed at
least once. The test
is at the end of the
loop

Flow of Control: for loops

for(initialization;
condition; increment)

{


doStuff();


...


}

doStuff

increment

Test

initialization

The break and continue statements

for(int i=0;i<10;i+=2)

{

for(
init
;
cond
;
incr
)

{


.... some code ...


if(condition1) break;


...


if(condition2) continue;

}

}



break: exits the inner loop



continue: jump to the end
of the inner loop and
loops around again

Flow of Control: For Loops (cont’d)

for(int i=0;i<10;i++) {}


for(;i;i
--
) {}


for(;condition;) {}



for(;;) {}


Flow of Control: Labels and the
dreaded Goto


You probably were not taught
about the goto statement



If you were, you were told that
it was bad



use break, continue, exit(n)
instead




Use it only in emergencies ....

Flow of Control: An example of goto


for ( ...)


for(...)


{


if(errorCond) goto errorLabel;


}


errorLabel:


fprintf(stderr,”Bad mojo


quitting program”);

I/O





Java has no I/O commands. All I/O is
performed using library functions


Output to the Console

//Unformatted Output

System.out.println(value1+val2+val3);


//Formatted I/O

String name="John Smith";

int age=20;

System.out.printf(“Name: %
-
20s: Age: %4d
\
n” ,


name,age);



41

Frequently
-
Used Specifiers


Specifier Output





Example

%b



a boolean value





true or false


%c

a character





'a'


%d



a decimal integer



200


%f

a floating
-
point number



45.460000


%e

a number in standard scientific notation

4.556000e+01

%s




a string






"Java is cool"



int count = 5;

double amount = 45.56;

System.out.printf("count is %d and amount is %f", count, amount);



display count is 5 and amount is 45.560000


items

String name;

int age=20;

//Wrap a Scanner around the input console

Scanner myScanner=new Scanner(System.in);


//Input a string

System.out.print("Name: "); //Prompt

name=myScanner.nextLine();


//Input an integer

System.out.print("Age: "); //Prompt

age=myScanner.nextInt();


System.out.printf("Name: %
-
20s: Age: %4d
\
n", name,age);

Input from the Console

String name;

int age=20;

//Wrap a Scanner around the input console

Scanner myScanner=new Scanner(new File(“c:
\
\
myData.txt”));


//Input a whole line as a String

name=myScanner.nextLine();


//Input an integer

age=myScanner.nextInt();


System.out.printf("Name: %
-
20s: Age: %4d
\
n", name,age);

Input from a file

Input from a web site on the Internet


String name;


int age;



//This looks complicated


but only at first. All it is doing is creating a connection

//to a remote file!

Scanner myScanner=new Scanner(


(new URL("http://munro.humber.ca/~king/abc.txt"))


.openConnection().getInputStream());


//The rest is exactly the same

name=myScanner.nextLine();

age=myScanner.nextInt();

System.out.printf("Name: %
-
20s: Age: %4d
\
n", name,age);

Getting Input from Input Dialog Boxes


String input =
JOptionPane.showInputDialog(


"Enter an input");






46

(GUI) Confirmation Dialogs

int option = JOptionPane.showConfirmDialog


(
null
, "Continue");