Written by students who passed Immediately available after payment Read online or as PDF Wrong document? Swap it for free 4.6 TrustPilot
logo-home
Essay

How to learn Java ?

Rating
-
Sold
-
Pages
18
Grade
A
Uploaded on
30-03-2023
Written in
2022/2023

How to learn java easily ?

Institution
Course

Content preview

Teach Yourself Java in 21 Minutes
Department of Computer Science, Lund Institute of Technology
Author: Patrik Persson Contact:

This is a brief tutorial in Java for you who already know
another object-oriented language, such as Simula or C++.
The tutorial is organized as a number of examples showing
the details of Java. The intention is to give you enough
information about the Java language to be able to follow the
course in real-time programming.
There are books claiming to teach you Java in 21 days, but
since you already know object-orientation your learning
time will probably be closer to 21 minutes – hence the title.
This document may be freely copied and distributed for non-commercial use. In case of
changes or enhancements, title, department, author, and contact must be preserved, and
changes that are redistributed or made public must be mailed to the contact above.


Table of contents
1 Simple declarations and expressions .......................................................................3
1.1 Simple declarations..............................................................................................................3
1.2 Numeric expressions and assignments.................................................................................3
1.3 Type conversion (casting) ....................................................................................................4
2 Statements ................................................................................................................4
2.1 If statements and boolean expressions .................................................................................5
2.2 While and for statements .....................................................................................................6
3 Classes and objects ..................................................................................................6
3.1 Classes..................................................................................................................................7
3.2 Methods................................................................................................................................7
3.3 Using objects........................................................................................................................8
3.4 Parameters to classes: constructors ......................................................................................8
3.5 The main method .................................................................................................................9
3.6 Inheritance............................................................................................................................9
3.7 Interfaces and listeners.......................................................................................................10
4 Exceptions..............................................................................................................11
4.1 Catching exceptions ...........................................................................................................11
4.2 Throwing exceptions..........................................................................................................12
4.3 Declaring new exceptions ..................................................................................................12
4.4 Unchecked exceptions........................................................................................................13
5 Miscellaneous ........................................................................................................13
5.1 Comments ..........................................................................................................................13
5.2 Using packages ..................................................................................................................13
5.3 Arrays.................................................................................................................................14
5.4 Writing to the terminal.......................................................................................................14
6 A complete Java program ......................................................................................15

Teach Yourself Java in 21 Minutes 1

,What is Java, and why?

The Java programming language was developed at Sun Microsystems and originally
became popular as a language for Internet applications (applets). Such applets are embed-
ded within WWW pages and executed in the user’s browser. A special format called byte
code is used instead of ordinary machine code, and by using a special Java interpreter pro-
gram that code can be executed on any computer. Such an interpreter is called a Java Vir-
tual Machine (JVM) and is available for most modern computer systems.

(There is nothing about the Java language itself that enforces the byte code technique –
there are actually some compilers who generate real machine code, known as native code,
directly.)

The Java language is not limited to Internet applications. It is a complete general object-
oriented language and can be used to develop many kinds of applications. Although the
syntax of Java is very similar to that of C++, many complicated and error-prone features of
C++ have been removed. The result can be described as a Simula with C++ syntax.

Sun Microsystems (who created the Java language) provide free tools for developing Java
software. The Java home page <http://java.sun.com> has links to Java compilers for
most computer systems (such as Unix and Microsoft Windows), as well as a lot of docu-
mentation. It is possible to download a Java compiler and use it for free.

Compiling and running Java programs

In Java, every source file usually contains exactly one class. The file must have the same
name as the class; a class named TurtleMaze would be stored in the source file
TurtleMaze.java. This source file can then be compiled using the javac compiler:

% javac TurtleMaze.java

The output of the compiler is a file with the same name as the source file, but with the
extension .class instead of .java (i.e., TurtleMaze.class in the above example). That
class file contains the byte code mentioned earlier, so it cannot be executed right away.
Instead it is executed using the JVM (byte code interpreter) as follows:
% java TurtleMaze

This command loads the TurtleMaze class and executes its main method (that is, starts
the program). If the TurtleMaze class in turn uses other classes, these are loaded automat-
ically when needed.

Since every class should be in its own file, several files can need to be recompiled at the
same time. The javac compiler has a special option -depend to compile all files that
depend on a particular file. The command
% javac -depend TurtleMaze.java

will compile not only TurtleMaze.java, but also all changed files it depends upon.




Teach Yourself Java in 21 Minutes 2

, Finding out more about Java

Many details of the Java language have been left out in this tutorial. If you want to know
more about the Java programming language, refer to one of the following sources:
• Per Holm: Objektorienterad programmering och Java. Studentlitteratur, 1998.
• Mary Campione and Kathy Walrath: The Java Tutorial (second edition). Addison-Wes-
ley, 1998. Also available on WWW:
<http://java.sun.com/docs/books/tutorial/index.html>.

• Ken Arnold and James Gosling: The Java Programming Language (second edition).
Addison-Wesley, 1998.
• Sun Microsystems: Java Technology Home Page: <http://java.sun.com>. Includes
detailed documentation about the Java class libraries.

If you have a question about Java which this short tutorial does not answer, feel free to ask
any of the teachers in your course.


1 Simple declarations and expressions
This section shows how to declare and use variables of the simple types, such as integers
or booleans. Declarations and uses of object references are shown in Section 3 on page 6.

Note that declarations and statements can be mixed freely (in contrast to Simula and
Pascal).

1.1 Simple declarations
Java supports the usual set of simple types, such as integer, boolean, and real variables.
Here are a few of the most common ones:
int m, n; // Two integer variables
double x, y; // Two real coordinates
boolean b; // Either ‘true’ or ‘false’
char ch; // A character, such as ‘P’ or ‘@’

1.2 Numeric expressions and assignments
Numeric expressions are written in much the same way as in other languages.
n = 3 * (5 + 2);
x = y / 3.141592653;
n = m % 8; // Modulo, i.e. n is now (m mod 8)
b = true;
ch = ‘x‘;

Note: the assignment is written using ‘=’ as opposed to ‘:=’ in many other languages.
Another symbol, ‘==’, is used to compare two values to each other (see Section 2.1 on
page 5). If you try to compare two values using ‘=’ you will get an error.


Teach Yourself Java in 21 Minutes 3

Written for

Course

Document information

Uploaded on
March 30, 2023
Number of pages
18
Written in
2022/2023
Type
ESSAY
Professor(s)
Unknown
Grade
A

Subjects

$3.99
Get access to the full document:

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Get to know the seller
Seller avatar
balavj

Get to know the seller

Seller avatar
balavj Exam Questions
Follow You need to be logged in order to follow users or courses
Sold
-
Member since
3 year
Number of followers
0
Documents
1
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions