Download Beginning Java and/or J

Document related concepts
no text concepts found
Transcript
Beginning Java
and/or J# and/or C#
Dr. Burns
About this course, Instructor



A first course in computer languages
Which language??
Your instructor is familiar with Visual Basic
Java, C# and C++
How to reach me??




Office hrs: 11-11:45 T Th
Office phone: 742-1547
Email: [email protected]
Website: http://burns.ba.ttu.edu
Pedagogical Requirements




2 exams, each worth 20%
1 project worth 20%
Homework worth 20%
Presentations worth 20%

Students will be asked to present material in
class five times during the semester
Some 3-hour classes are organized
as…


1.5 hours of lecture by me (80-90 min)
1.5 hr lecture by one of you


.75 hr by one student (40 min)
.75 hr by another student (40 min)
A bit of History



Java was developed at Sun Microsystems
by a programmer named James Gosling
who originally had in mind a language to
control robots
June 1991 to 1995 was the development
period
It is platform-independent and will run on
any operating system or computer
More Java history


Development goals were to create a
language with a C/C++ like syntax, but with
WORA (Write Once, Run Anywhere)
The language was also made much simpler
than C++

No pointer variables, no pointer arrays, no
destructors, etc.
Java Standard

The Java standard is a de facto standard
(not one that is controlled by ANSI or ISO)
that is controlled by the Java Community
Process
Java Language Goals





It should be object-oriented
It should allow the same program to be executed
on multiple operating systems and
microprocessors using different machine
languages
It should contain built-in support for use of
computer networks
It should be executable from remote sources
It should be easy to use (Wikipedia)
J# Programming language



A transitional language to assist Java
programmers to transition to Microsoft’s .NET
platform
J# can work with Java bytecode as well as
source, so it can be used to transition applications
that use 3rd-party libraries
It was developed by the Hyderabad-based
Microsoft India Development Center at HITEC
City in India
Fundamental differences between J#
and Java



Both use the same general syntax
There are non-Java conventions in J# to
support the .NET environment
J# does not compile Java language source
code to Java bytecode and does not
support Java applet development or the
ability to host applets directly in a web
browser
Future of J#


J# is not considered a language on a par
with C# or VB.NET and does not have the
same level of support, samples or updates
as the other languages do
Nevertheless, J# is a usable .NET
language and has access to all the CLR
features
What is CLR?


CLR stands for Common Language
Runtime and is the virtual machine
component of Microsoft’s .NET platform. It
is Msft’s implementation of the Common
Language Infrastructure (CLI) standard.
Developers using the CLR write code in a
language such as C# or VB.Net.
History of J#




J# is Microsoft’s version of Java
J# will run only on Windows machines
Uses different methods names than Java
Within Visual Studio, it can be mixed and
matched with Visual C++, Visual Basic,
Visual C#, ASP.net, etc.
Which language works best for your
needs?

Let me suggest Java or Visual C#
Reserved Words/Objects in C# or Java



Only about 48 of them, compared to over
200 in Visual Basic
Is Object-oriented
But doesn’t support pointer variables, nor is
the programmer allowed to destroy objects,
unlike C++
Reserved Words









abstract
assert
boolean
break
byte
case
catch
char
const








continue
default
do
double
else
enum
extends
final
More reserved words









finally
float
for
goto
if
implements
import
instanceof
int









interface
long
native
new
package
private
protected
public
return
Still more reserved words







short
static
strictfp
super
switch
synchronized
this







throw
throws
transient
try
void
volatile
while
Notice….



None of the reserved words begins with an
uppercase character (both C# and Java
have case sensitive identifiers)
The reserved words shown in RED are also
C# reserved words
C# has 75 reserved words (listed on page
31 of your text)
Identifiers

These are words you choose for…






Class libraries (Java)
Classes
Namespaces (C#)
Variables
Methods
They cannot be reserved words
More on Identifiers




Class names should start with uppercase
Method and variable names should start
with lower case
No spaces
You can only use letters (lower and
uppercase), digits and the underscore
character
Some bad class names





An employee
Inventory Item
Class
2009Budget
Phone#
Space char is bad
Space char is bad
a reserved word
can’t begin with digit
# is illegal
Operators

Supports only the basic operators







! {the logical NOT}
* / % {multiplication, division, modulus}
+ {addition, subtraction}
> < >= <= {Relational}
== !=
{Equality}
&&
{Logical AND}
||
{logical OR}
Operators in C++ that are not
supported

++
--

Many others
Object Technology


Inheritance -- gives rise to REUSE
Encapsulation





Information hiding
Polymorphism
Contains attributes (data and methods)
Objects are called classes
No support for procedural programming—which is
the direct opposite to object-oriented
programming
Class—an object




Encapsulates both data and behaviors
Behavior is delineated with a method—a
collection of code
Consider the class Sedan(…)
Contained within it are constants like


Wheels = 4, chassis = 1, engine = 1, doors = 4
Color = RED
The class Sedan() has methods that
describe its behaviors





Accelerates(…)
Slows_down(…)
Consumes_gas(…)
Creates_pollution(….)
Each of these is a method, a collection of
code that acts as a group to produce the
behavior
Method

A self-contained block of program code,
similar to a procedure or subroutine
Procedural vs. Object-oriented
programming

Procedural programming



What I did using Fortran as a software engineer
working for the Boeing Company in the 1970’s
Just long lists of code
Object-oriented programming—organizes
code into objects—allows for reuse
The generic structure for an Object



First, a list of all the possible header
declarations—to support polymorphism
Second, a list of all the data types and their
initializations
Third, a list of all the methods, coupled with
their code declarations
Objects and their instantiations




OBJECT
truck
automobile
animal
INTSTANTIATION
fordF150
chevyMalibu
dog
Header declarations—the same in Java
and C#

Java:
public static void main(String[] args)
C#:
static void main(String[] args)
public static void main(String[] args)

Notice—no semicolon after header dec




What is main(String[] args)?




Is it a class?
Is it an instance of a class?
Is it a method?
Is it a function?
What do the reserved words mean?



public—the method is accessible to
everyone, not just other methods inside
the class
static—the method stays alive and
functional while the program is running
void—the method does not return a value
through its name
Instantiations



We use the keyword ‘new’ to create an
instantiation of an object:
New automobile Toyota
Here Toyota is an instance of the class
automobile
Inheritance



The ability of a class to inherit the data and
methods of another previously defined
class.
my1997ToyotaCorolla() extends Sedan()_
Inherits all of the data and methods of
Sedan()—reuses them—they don’t have to
be redefined
Polymorphism



Literally means ‘many forms’
Allows methods to be used differently in
different contexts
Run() can be a footrace, an execution of a
computer program, or a business process


It is correctly understood in context
The method eat() will be different depending on
what kind of animal is eating
Code for a Class
Public class First
{
public static void main(String[] args)
{
System.out.println(“First Java app”);
}
}
The Membership Operator
Is simply the .
 Examine……………
System.out.println(“First Java app”);
 We see that println() is a method within a
class out which is a class within a class
called System
 Unfortunately, J# and C# do not use these
names

What is (“First Java app”)?




“First Java app” is a literal string of
characters – placed within double quotes
This string is an argument of the method
System.out.println()
Methods are always followed by ()
parentheses
What is placed within those parentheses
are called arguments
Some TLA’s—Three-letter Acronyms







IDE – Integrated Development Environment
JVM – Java Virtual Machine
JRE – Java Runtime Environment
SDK—Software Development Kit
API—Application Programming Interface
CLR—Common Language Runtime
(Microsoft) is part of .NET
WPF—Windows Presentation Foundation
Integrated Development Environments


IDE – Integrated Development
Environment—what Visual Studio and
Eclipse provide—usually consisting of
editor, parser, interpreter
But sometimes the IDE is capable of
compiling and linking—creating a stand
alone executable– ending with .exe
What is the sequence leading to
execution?

In any language, it is
1.
2.
3.
Enter code in the editor– editor checks for
errors -- result is a text file with the extension
of .java or .J#
Compile code into machine language – result is
a binary file with the extension .bin
Link code to other compiled objects to create a
stand alone executable – result is a binary file
with the extension .exe
There are many variations on this
sequence ….


Enter code – check for errors -- IDE saves
the result as a text file.
IDE does a ‘quick’ interpret and execute
without creating a stand alone executable
What is the difference between
interpretation and execution?



No difference as far as the output of the
program is concerned.
With interpretation, a line of source code is
interpreted and executed without its being
compiled, linked and executed as a stand
alone module
This is very slow compared to stand alone
execution
Microsoft .NET
Common Language Runtime diagram
What about the sale of commercial
products?

Do you want these products interpreted or
executed from a stand alone executable?
Java Compilations




Java compilers typically produce bytecode
rather than binary (machine language)
This makes them platform independent
JVM’s and JRE’s then interpret the byte
codes upon execution
Because of this, Java programs run slower
than native executables
Freeware



A Java IDE (Java SE 6) that is free can be
found at http://java.sun.com
A Java JRE that is free can be found at
http://java.sun.com
A downloadable Java Tutorial is available
at http://java.sun.com/docs/books/tutorial/
What about Applets?

Applets are scripts, segments of Java code
that get passed down with a web page.
Your browser has a Java interpreter that
executes the code line by line, creating the
animation in a box of the page
Comments




// the entire line is a comment
/* begins a comment that can continue over
several lines and ends with */ this I s called
a block comment
C# supports all of the above
/** is a Javadoc comment that works with a
special documentation program; must end
with */
More sample programs
Import Javax.swing.JOptionPane;
Public class FirstDialog
{
public static void main(String{} args)
{
jOptionPane.showMessageDialog(null,”First
Java dialog”);
System.exit(0);
}
}
Data type Declarations





Specify the type of data and the length of
the data item in bytes
int, short, long
float, double
boolean
enum
Floating Point Representation



Internal representation
Floating-point numbers are typically packed into a
computer datum as the sign bit, the exponent
field, and the significand (mantissa), from left to
right. For the IEEE 754 binary formats they are
apportioned as follows:
TypeSignExponentExponent
biassignificandtotalHalf (IEEE 7542008)15151016Single181272332Double1111023
5264Quad11516383112128
Data Types -- Integer



Int – the default declaration – 4-byte integer
Short—2-byte integer
Long—8-byte integer
Floating Point


Float—a 4-byte floating point number
Double—an 8-byte floating point number