Java 1
Java 1
What Is Java?
• Java is an object-oriented programming language
developed by Sun Microsystems. the Java language was
designed to be small, simple, and portable across
platforms and operating systems.
Applets and Applications
• Java applications fall into two main groups: applets and
applications.
Applets, are Java programs that are downloaded and
executed by a Web browser on the reader‟s machine.
Applets depend on a Java-capable
browser in order to run (although they can also be viewed
using a tool called the applet viewer.
Java applications are more general programs written in
the Java language. Java applications don‟t
require a browser to run, and in fact, Java can be used to
create most other kinds of applications
that you would normally use a more conventional
programming language to create.
Creating a Java Application
• your Java source files are created in a plain text editor, or
in an editor that can save files in plain ASCII without any
formatting characters.
• class HelloWorld {
public static void main (String args[]) {
System.out.println(“Hello World!”);
}
}
import java.awt.Graphics;
Floating numbers
float (32 bits, single-precision)
double(64 bits, double-precision).
size = 14;
tooMuchCaffiene = true;
• Each of these variables can then hold only instances of
the given class. As you create new classes,you can
declare variables to hold instances of those classes (and
their subclasses) as well.
• Java does not have a typedef statement (as in C and
C++). To
declare new types in Java, you declare a new class; then
variables can be declared to
be of that class‟s type.
Comments
• /* Multi Lines Comment*/
• // single line comment
• Example
• String txt = "Hello World";
System.out.println(txt.toUpperCase()); // Outputs "HELLO
WORLD"
• System.out.println(txt.toLowerCase()); // Outputs "hello
world"
String function
• Finding a Character in a String
• The indexOf() method returns the index (the position) of
the first occurrence of a specified text in a string (including
whitespace):
• Example
• String txt = "Please locate where 'locate' occurs!";
System.out.println(txt.indexOf("locate")); // Outputs 7
Operators
• Arithmetic operators
+ Addition 3 + 4
– Subtraction 5 – 7
* Multiplication 5 * 5
/ Division 14 ÷ 7
% Modulus 20 % 7
•
Example
class ArithmeticTest {
public static void main (String[] args) {
short x = 6;
int y = 4;
float a = 12.5f;
float b = 7f;
System.out.println(“x is “ + x + “, y is “ + y);
System.out.println(“x + y = “ + (x + y));
System.out.println(“x % y = “ + (x % y));
}
}
Assignment operators
x += y x = x + y
x –= y x = x – y
x *= y x = x * y
x /= y x = x / y
++x; // x += 1
−−x; // x -= 1
++x; // pre-increment
−−x; // pre-decrement
x++; // post-increment
x−−; // post-decrement
Comparisons operators
Comparison operators are used to compare two values:
!= Not equal x != y
• Syntaxe
• variable = (condition) ? expressionTrue : expressionFalse;
•
Example
• int time = 20;
• String result = (time < 18) ? "Good day." : "Good evening.";
• System.out.println(result);
Switch Conditionals
• Syntax
• switch(expression) {
• case x:
• // code block break;
• case y:
• // code block break;
• default:
• // code block
}
Switch Conditionals
• int day = 4;
• switch (day) {
• case 6:
• System.out.println("Today is Saturday"); break;
• case 7:
• System.out.println("Today is Sunday");
• break;
• default:
• System.out.println("Looking forward to the Weekend");
} // Outputs "Looking forward to the Weekend"
Switch Conditionals
• String oper = “*”;
• switch (oper) {
case „+‟: addargs(arg1,arg2); break;
case „*‟: multargs (arg1,arg2); break;
case „-‟: subargs (arg1,arg2); break;
case „/‟: divargs(arg1,arg2); break;
• Example
• int i = 0;
• while (i < 5) {
• System.out.println(i);
• i++;
}
do-while Loop
• do
{
Code
}while (Condition);
Example
• int x = 1;
do {
System.out.println(“Looping, round “ + x);
x++;
} while (x <= 10);
for Loops
• Example
for (i = 0; i < 10; i++);
System.out.println(“Number is”+i);
• Example
for (int i=1 ; i <= 10; i++ )
{
for (int j=1 ; j <= 10; j++ ) // loop prints multiplication
table
System.out.print (" i*j =" + i*j);
System.out.println (); //print empty line
}
Arrays, Conditionals, and Loops
• Arrays
•
Declaring Array Variables
int array1[ ] = new int[9];
int [ ] array1 = new int[9];
• Example
• int b[ ][ ];
b = new int[3][4];
int b[ ][ ] = { { 1, 2 }, { 3, 4 } };
• Example
String[] chiles = { “jalapeno”, “anaheim”,“serrano,”,
“habanero,” “thai” };
Accessing Array Elements
• Example
• int ages[ ]= {20, 18, 34, 42, 28};
• Example
• String arr[] = new String[10];
int len = arr.length // returns 10
Math.ceil(5.1) 6
Math.ceil(-5.1) -5
Math.floor(5.1) 5
Math.floor(-5.1) -6
Math.max(7,6) 7
• Math.min(-7,-8) -8
• Math.pow(6,2) 36
Math.sqrt(9) 3
Math.random() 0.24421
Example
• Example (receive two numbers and print sum)
• import java.util.Scanner;
public class Addition
{
public static void main( string args[] )
{
Scanner input = new Scanner( System.in );
int number1;
int number2;
int sum;
System.out.print( "Enter first integer: " );
number1 = input.nextInt();
System.out.print( "Enter second integer: ");
number2 = input.nextInt();
sum = number1 + number2;
System.out.printf( "Sum is %d\n", sum );
}
}