0% found this document useful (0 votes)
73 views

Comp Prog Notes

The document provides an overview of the Java programming language and platform. It discusses that Java is an object-oriented, platform-independent language that is compiled to bytecode and run on a Java Virtual Machine. It also describes Java's key features like being portable, secure, robust and having a large standard API library. The document then discusses Java packages and how they help organize and avoid naming collisions between classes.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Comp Prog Notes

The document provides an overview of the Java programming language and platform. It discusses that Java is an object-oriented, platform-independent language that is compiled to bytecode and run on a Java Virtual Machine. It also describes Java's key features like being portable, secure, robust and having a large standard API library. The document then discusses Java packages and how they help organize and avoid naming collisions between classes.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

~the Java Programming language

is a high level language that can be characterized by all of the following


buzzwords.
*Simple *Architecture neutral
*Portable *Object Oriented.
*Distributed *Multithreaded
*Dynamic * High Performance
*Robust *Secure
all source code is first written in plain text files ending with the java
extension. Those source files then compiled into class not contain code
that is native to your processor, it instead contains byte codes - the
machine language of java virtual machine The java launcher tool then
runs your application with an instance of the jar Virtual Machine
Notepad=.text
Java on Notepad Java =.java
. class->Complied
~An overview of the Software development process :
Myprogram.java->
complier->
Myprogram.class->
Java vm ->
My program
Because the java virtual machine is available on many files different
operating system, the same files are capable of running on Microsoft
Windows, the Solaris Operating system Macos. Some virtual machines,
such as the java se Linus the or Hot spot at a glance perform additional
steps at run time to give your application a performance boost. This
includes various (hacks) tasks such as finding performance bottle necks
and recompiling (to native code) frequently used sections of code.
~The Java platform
is a hardware or software environment in which a program runs. Were
already mentioned some of the most popular platforms like microsoft
windows, Linux, Solaris os, and Mac os. Most platforms can be
described as a combined) combination of the operating system and
underlying hardware. The Java platform differs from most of the) other
platforms in that its a software only platform that runs on top of other
hardware-based platforms
The Java platform has two components:
The Java Virtual Machine
The Java Application Programming interface (API)
~APL
is a lange collection of a readdy made software componentsthat
provide many useful capabilities . 4 is grouped into Starling
libraries of related classes and interfaces, these libraries are known as
packages.

Myprogram.java
API
Java Virtual Machine java platform
Hardware based Platform

-As a platform independent environment, the Java platform. can be a


bit slower than native code. However advances in compiler and virtual
machine technologies are bringing performance close to that of native
made without threatening Portability.
The terms "Java Virtual machine" and "NUM" mean a Virtual Machine
for the Java platform.
What Can Java Do for us?
The general purpose, high level java Programming language is a
powerful software platform, Every full implementation of the java
platform gives you the following features:
Development tools
provide everything you'll need for compiling running, monitoring
debugging and documenting your application. As a new developer, the
main tools you'll be using are Javac compiler, the java launcher, and
javadoo documentation tool.
Application Programming Interface (API)
The API provides the languge) care functuality of the java programming
language. It offers a wide array of useful classes ready for use in your
own applications, spans everything from basic objects, to networking
and security, to XML generation and data base access and more. The
core API is very large, to get an overview of what it contains consult the
java platform standard Edition 8 Documentation.
Development Technologies.
The JDK software provides standard mechanism such as the java web
start and Java Plug-In software for deploying your application to end
users
user Interface user toolkits.
The JavaFX swing and Java 2D toolkits make it possible to create
sophisticated (Grana) Graphical user interfaces (GUIS)
Integration Libraries
integration Libraries such as the java IDL APL JDBC APL, Java Naming
and directory Interface (INDO APL, Java RMI, and Java Remote Method
invocation over internet Inter URB Protocol Technology Wava RMI-Ilop
Technology enable database access and manipulation of remote
objects.
How will java Technology change My Life
we cant promise you fame, fortune or even a job if you learn your
programs better and a the java programming language still, it is likely to
make requires less effort than other following languages. We believe
that Java technology will help your do the following
Get started quickly
Although the java programming language is a powerful object-oriented
language, its easy to learn especially for programmers already familiar
with C or C++.
Write less code
comparisons of program metrics cclass counts a method counts and so
on) suggest that a program written a in the java programming language
can be four times smaller than the same program written in C++.
Write better code
The java Programming language encourages good coding practices,
and automatic garbage collection helps you (with) avoid memory leaks.
Its object orientation, and its (wide-ranging) JavaBears in component
architecture, and its wide-ranging, easily extendible APL let you rescue
existing, tested code and introduce fewer bugs.
Develop programs more quickly:
The Java programming language is simpler than C++, and as such, your
development time could be up to twice as fast when writing in it. Your
programs will also require fewer lines of code.
Avoid platform dependencies: You can keep your program portable by
avoiding the use of libraries written in other languages.
Write once, run anywhere: Because applications written in the Java
programming language are compiled into machine-independent
bytecodes, they run consistently on any Java platform.
Distribute software more easily: With Java Web Start software, users
will be able to launch your applications with a single click of the mouse.
An automatic version check at startup ensures that Users are always up
to date with the latest version of your software. If an update is
available, the Java Web Start software will automatically update their
installation
JAVA FRAMEWORK
The "Hello World!" Application To write your first program, you'll need:
• The Java SE Development Kit 8 (JDK 8)
You can download the Windows version now.
(Make sure you download the JDK, not the JRE.
Consult the installation instructions.
• A text editor
In this example, we'll use Notepad, a simple editor
included with the Windows platforms. You can easily
adapt these instructions if you use a different text
editor.
These two items are all you’ll need to write your first
application.
Creating Your First Application Your first application,
HelloWorldApp, will simply display the greeting "Hello world!'. To
create this program, you will:
Create a source file
source file contains code, written in the Java programming language,
that you and other programmers can understand. You can use any text
editor to create and edit source files
Compile the source file into a .class file
The Java programming language compiler (javac) takes your source file
and translates its text into instructions that the Java virtual machine
can understand. The instructions contained within this file are known as
bytecodes.

Run the program


The Java application launcher tool (java) uses the java virtual machine
to run your application.

First, start your editor. You can launch the Notepad editor
from the Start menu by selecting Programs > Accessories
Notepad. In a new document, type in the following code

/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!' to standard output.
*
class Hello World App
public static void main(String[] args) {
System.out.println("Hello World!"); // Display the string,
}
}
Save the code in a file with the name Hello World App java To do this in
Notepad, first choose the File > Save As menu item. Then, in the Save
As dialog box
1.Using the Save in combo box, specify the folder (directory) where
you'll save your file. In this example, the directory is myapplication on
the C drive.
2.In the File name text field, type "Hello World App java", including the
quotation marks,
3.From the Save as type combo box, choose Text Documents (*.txt).
4.In the Encoding combo box, leave the encoding as ANSI

JAVA PACKAGES
Packages are nothing more than the way we organize files into different
directories according to their functionality, usability as well as category
they should belong to. An obvious example of packaging is the JDK
package from SUN (Java.xxx.yyy)

Basically, files in one directory (or package) would have different


functionality from those of another directory. For example, files in
java.io package do something related to I/O, but files in java.net
package give us the way to deal with the Network. In GUI applications,
it's quite common for us to see a directory with a name "ui" (user
interface), meaning that this directory keeps files related to the
presentation part of the application. On the other hand, we would see
a directory called "engine", which stores all files related to the core
functionality of the application instead.
Packaging also help us to avoid class name collision when we use the
same class name as that of others. For example, if we have a class name
called "Vector", its name would crash with the Vector class from JDK.
However, this never happens because JDK use java.util as a package
name for the Vector class (Java.util.Vector).

So our Vector class can be named as "Vector" or we can put it into


another package like com.mycompany.Vector without fighting with
anyone. The benefits of using package reflect the ease of maintenance,
organization, and increase collaboration among developers.
Understanding the concept of package will also help us manage and use
files stored in jar files in more efficient ways

How to create a package


Suppose we have a file called HelloWorld.java, and we want to put this
file in a package world. First thing we have to do is to specify the
keyword package with the name of the package we want to use (world
in our case) on top of our source file, before the code that defines the
real classes in the package, as shown in our Hello World class on the
next slide:
// only comment can be here
package world;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
Setting up the CLASSPATH
We put the package world under C:. So we just set our CLASSPATH as:
set CLASSPATH=.;C:\; We set the CLASSPATH to point to 2 places, .
(dot) and C:\ directory.
When compiling HelloWorld class, we just go to the world directory
and type the command: C:\world\javac HelloWorld.java

The reason is right now the HelloWorld class belongs to the package
world. If we want to run it, we have to tell JVM about its fully-qualified
class name (world. HelloWorld) instead of its plain class name
(HelloWorld).
C:\world>java world.HelloWorld
C:\world>HelloWorld
Note: fully-qualified class name is the name of the java class that
includes its package name Skyworth

Activity: Use package


1. Create a subfolder in your folder and name it as "world"
2. Transfer all your files from your folder in this subfolder.
3. On the cmd prompt, type set CLASSPATH=.;C:\"yoursurname";
4. Access HelloWorldApp.java in different directory
Java Data Types
The Primitive Types Java defines eight primitive types of data: byte,
short, int, long, char, float, double, and boolean. The primitive types
are also commonly referred to as simple types. These can be put in four
groups:
• Integers This group includes byte, short, int, and long, which are for
whole-valued signed numbers.
• Floating-point numbers This group includes float and double, which
represent numbers with fractional precision.
• Characters This group includes char, which represents symbols in a
character set, like letters and numbers.
• Boolean This group includes boolean, which is a special type for
representing true/false values.
Integers
Java defines four integer types: byte, short, int, and long. All of these
are signed, positive and negative values. Java does not support
unsigned, positive-only integers. Many other computer languages
support both signed and unsigned integers. However, Java's designers
felt that unsigned integers were unnecessary. Specifically, they felt that
the concept of unsigned was used mostly to specify the behavior of the
high-order bit, which defines the sign of an integer value. Java manages
the meaning of the high-order bit differently, by adding a special
"unsigned right shift" operator. Thus, the need for an unsigned Integer
type was eliminated.
The width of an integer type should not be thought of as the amount of
storage it consumes, but rather as the behavior it defines for variables
and expressions of that type. The Java run-time environment is free to
use whatever size it wants, as long as the types behave as you declared
them. The width and ranges of these integer types vary widely, as
shown:
Long [64] -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
int [32] -2,147,483,648 to 2,147,483,647
short [16] -32,768 to 32,767
byte [8] -128 to 127

Floating-Point Numbers
Floating-point numbers, also known as real numbers, are used when
evaluating expressions that require fractional precision. For example,
calculations such as square root, or transcendentals such as sine and
cosine, result in a value whose precision requires a floatingpoint type.
Java implements the standard (IEEE-754) set of floating-point types and
operators. There are two kinds of floating-point types, float and
double, which represent single-and double-precision numbers,
respectively. Their width and ranges are shown here:
Characters
In Java, the data type used to store characters is char. However, C/C++
programmers beware: char in Java is not the same as char in C or C++.
In C/C++, b is 8 bits wide. This is not the case in Java. Instead, Java uses
Unicode to represent characters. Unicode defines a fully international
character set that can represent all of the characters found in all human
languages. It is a unification of dozens of character sets, such as Latin,
Greek, Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more.

At the time of Java's creation, Unicode required 16 bits. Thus, in Java


char is a 16-bit type. The range of a char is o to 65.536. There are no
negative chars. The standard set of characters known as ASCII still
ranges from o to 127 as always, and the extended 8-bit character set,
ISO-Latin-1, ranges from oto 255. Since Java is designed to allow
programs to be written for worldwide use, it makes sense that it would
use Unicode to represent characters. Of course, the use of Unicode is
somewhat inefficient for languages such as English, German, Spanish,
or French, whose characters can easily be contained within 8 bits. But
such is the price that must be paid for global portability
Booleans
Java has a primitive type, called boolean, for logical values. It can have
only one of two possible values, true or false. This is the type returned
by all relational operators, as in the case of a < b. boolean is also the
type required by the conditional expressions that govern the control
statements such as if and for..
Literals
Literals are the representation of values. Integer Literals - Integers are
probably the most commonly used type in the typical program. Any
whole number value is an integer literal.
Floating-Point Literals - Floating-point numbers represent decimal
values with a fractional component. They can be expressed in either
standard or scientific notation. Standard notation consists of a whole
number component followed by a decimal point followed by a
fractional component.
Boolean Literals
- Boolean literals are simple. There are only two logical values that a
boolean value can have, true and false. The values of true and false do
not convertinto any numerical representation. The true literal in lara
does not equal 1, nor does the false literal eoual O. In lava, the Boolean
literals can only be assigned to vanables dedared as boolean or used in
expressions with Boolean operators
Variables
The variable is the basic unit of storage in a java program. A variable is
defined by the combination of an identifier, a type, and an optional
initializer. In addition, all variables have a scope,
which defines their visibility, and a lifetime. These elements are
examined next.
Declaring a Variable
in lava, all variables must be declared before they can be
used. The basic form of a variable
declaration is shown here:
type identifier [= value][, identifier[= value]…]
Here, type is one of Java's atomic types, or the name of a class or
interface (Class and interface types are discussed later in Part of this
book. The identifier is the name of the variable. You an initialize the
variable by specifying an equal sign and a value. Keep in mind that the
initialization expression must result in a value of the same for
compatible) use a comma-separated list.
ere are several examples of variable declarations of various types. Note
that some include an initialization
int a,b,c // declares three ints, a, b, and c.
int = 3, e, f = 5, // initializing il declares three more ints,
initializing
// d and f.
byte z = 22; // initializes z.
double pi = 3.14159; // declares an approximation of pi.
char x = 'x; // the variable x has the value X
The identifiers that you choose have nothing intrinsic in their names
that indicates their type. Java allows any properly formed identifier to
have any declared type.
ACTIVITY
Make a Java program that will assign given data to variables, compute
and display using the format shown below. Save the program as
GrossPay.java. Compile and execute the program,
A. Compute for
Gross Pay = Days Worked Daily Rate
Net Pay = Gross Pay- (Tax+SSS+CA)
Employee Name : ”Your name”
Exployee Number :19341
Employment Status :R
Department :Accounting

Days Worked :22 days


Daily Rate :300.18
Gross Pay :XXXX.XX

Less Tax :270.00


SSS :120.00
CA :150.00
Het Pay :XXXX.XX
public class Grosspay
{
public static void main(String{}arg) {
/* declare variables */
String ename,dept;
char estat;
int num,dwork;
double drate,gpay, tax,sss.ca, npay:
/* assign data*/
ename="Your Name'; num=14344; estat='R’;
dept="Senior High School"; dwork=22; drate=300.18;
tax=270.25; sss=120.50; ca=450 75;
/*computation*/
gpay=dwork drate;
npay=gpay-(tax+sss+ca);
/*dispislay*/
System.out.println("");
System.out.println(Employee Name "+ename);
System.out.println("Employee Number "+num);
System.out.println("Employment Status"+estat);
System.out.println("Department "+dept);
System.out.println("Days Worked "+dwork);
System.out.println(“Daily Pale "+drate);
System.out.println("Gross Pay "+gpays);
System.out.println(less Tax "+tax);
System.out.println(" SSS "+sss);
System.out.println(" CA "+ca);
System.out println(“ et Pay "+npays);

You might also like