Java Is A Strongly Typed
Language
Presented By:
Priyanka Das
ID: 20104014
History of Java
First version released in 1995
Eight major versions released since then
– JDK 1.0 (1996) JDBC, Distributed Objects
– JDK 1.1 (1997) New Event Model
– J2SE 1.2 (1998) Swing
– J2SE 1.3 (2000) Cleanup
– J2SE 1.4 (2002)
– J2SE 5.0 (2004) Generics
– J2SE 6.0 (2006)
– J2SE 7.0 (2011)
Five Design Principles of Java
simple, object-oriented and familiar
robust and secure
architecture-neutral and portable
execute with "high performance
interpreted, threaded, and dynamic
What is/isn’t Java?
Basically, compared to C Java is a relatively high-
level language with many built-in features for
portably doing useful things such as:
– Multithreading
– Writing distributed programs
– Writing GUI clients
– Error handling
– Extending Web servers
– Embedding programs in Web browsers
– Connecting to commercial databases
Java vs. C
Equally importantly, Java has many core
language features that make it much more
natural to express abstraction, develop
software frameworks, etc.
Also, many core language features which
ease debugging and promote reuse.
Compiling/running first java
program
Create source code file (call it for example
[Link]).
To compile:
prompt >> javac [Link]
This produces byte code file named
[Link]
To run:
prompt >> java MyFirstProgram
Observations
.class file is not machine code. It is intermediate
form called Java Byte code. Can run on any
platform as long as platform has a Java Virtual
Machine (JVM).
The second step on previous slide invokes the JVM
to interpret the byte code on the given platform.
In theory, byte code can be moved to another
platform and be run there without recompiling –
this is the magic of applets.
Leave off the .class part when invoking the JVM.
Observations
This is an old-fashioned command-line
program. Java also supports GUI
applications and web-browser hosted
programs called applets.
After the first couple of weeks we will use
graphical rather than scripting front-ends.
Single-threaded program
For a single thread of execution, each line
of code is executed sequentially (as in C).
Each statement is terminated with a
semicolon.
Unlike C, declarations can occur anywhere
within a program.
Basic operators, control statements almost
exactly like C. A few minor differences.
Best to just look at some examples.
Java Data Types
Sizes fully specified by Java standard.
Java is a very strongly typed language
Integer types
– int (4 bytes signed)
– short (2 bytes signed)
– long (8 bytes signed) use suffix L (eg 1000000000L)
– byte (1 byte signed)
Floating-point types
– float (4 bytes) use suffix F (eg 1.28F)
– double( 8 bytes)
Additional Data Types
char
– Two-byte unicode
– Assignment with ‘ ‘
• e.g. char c = ‘h’;
boolean
– true or false
e.g. boolean x = true;
if (x){…};
Operators/Control Flow
Almost exactly like regular ANSI C.
+, *, -, /, %, ++, --, +=, etc.
==, !=, >, < , etc.
if statements, for loops, while loops, do loops,
switch statements, etc.
continue, break, return, [Link](0).
Read pp 54– in Core Java.
No need to spend class time going over these.
Scoping
Braces are used as in C to denote begin/end of
blocks
Be careful of the following:
int j = 0;
if ( j <1 ){
int k = 2;
…
}
k = 3; //Error! k inaccessible here
Declarations do not propogate upwards.
Adding datatypes -- classes
Java has handful of built-in datatypes just
discussed (int, float, etc.)
Just like in C, user typically creates own
homemade datatypes to work with particular
application (ie structs and enums).
In Java these are called classes.
Many class definitions come as a standard part of
the Java distribution. Most common Example is
String class.
Strings
Java provides a class definition for a type called
String
Since the String class is part of the [Link] package,
no special imports are required to use it (like a header
file in C).
Just like regular datatypes (and like C), variables of
type String are declared as:
String s1;
String s2, s3; //etc.
Note that String is uppercase. This is the Java
convention for classnames.
Strings
Initializing a String is painless
s1 = “This is some java String”;
Note that double quotes are required.
Memory is allocated dynamically.
Think of above method as shortcut for more
standard way (assuming s1 has been declared):
s1 = new String(“This is some java String”);
new operator required to create memory for new
String object.
String methods
Given a String object we can then access any
public String method or instance variable (field).
Best to think of analogy with C. Given a variable
of some struct type, we can access any of the
struct’s members. If one of these members is a
pointer to a function, we can essentially call a
function using the struct. ([Link](x,…))
In Java, this idea is taken quite a bit further, but
the above analogy is a good start.
String Examples
Best to see by way of example:
String s = new String(“Hello”);
Char c = [Link](3);
[Link](c);
Method charAt called on String object s
taking single integer parameter.
How might this look in a procedural
language with structures? (homework)
Thank You