Java Basic
Java Basic
Programming
Java Basic
• Originally called Oak, it was designed in 1991 for use in embedded chips in
consumer electronic appliances.
• It is employed for
• Web programming,
• The Java language specification and Java API define the Java standard.
• The Java language specification is stable, but the API is still expanding.
• Java is a full-fledged and powerful language that can be used in many ways.
• There are many versions of Java SE. Sun releases each version with a Java
Development Toolkit (JDK).
• For Java SE 6, the Java Development Toolkit is called JDK 1.6 (also known as
Java 6 or JDK 6).
• Use a Java development tool (e.g., NetBeans, Eclipse) - software that provides
an integrated development environment (IDE) for rapidly developing Java
programs.
• If there are no syntax errors, the compiler generates a bytecode file with
a .class extension.
• To store the radius, the program needs to declare a symbol called a variable.
• A variable has a name that can be used to access the memory location.
• Choose descriptive names: radius for radius, and area for area.
• To let the compiler know what radius and area are, specify their data types.
• Use the println method to display a primitive value or a string to the console.
• Use the Scanner class to create an object to read input from System.in:
• Example: You can assign any numerical value to radius and area, and
the values of radius and area can be reassigned.
• To use a variable, you declare it by telling the compiler its name as well as
what type of data it can store.
• Syntax:
datatype variableName;
• Examples :
int count;
double radius;
double interestRate;
• If variables are of the same type, they can be declared together, they are
separated by commas:
• For example:
int i, j, k;
• If a name consists of several words, concatenate all of them and capitalize the
first letter of each word except the first.
variable = expression;
• Example:
1. System.out.println(x = 1);
2. i = j = k = 1;
• Syntax:
• (2) if you have to change the constant value (e.g., from 3.14 to 3.14159
for PI), you need to change it only in a single location in the source code;
• (3) a descriptive name for a constant makes the program easy to read.
• The compiler allocates memory space for each variable or constant according
to its data type.
• Java provides eight primitive data types for numeric values, characters, and
Boolean values.
• Initialized to zero
Categories: 1. byte
Size: 1 byte
a. integer Range: -27 27 - 1
2. short
b. floating Size: 2 bytes
Range: -215 215 - 1
c. character 3. int
Size: 4 bytes
d. boolean Range: -231 231 - 1
4. long
Size: 8 bytes
Range: -263 263 - 1
• Initialized to zero
Categories:
a. integer
b. floating Size: 4 bytes
1. float Range: ±1.4 x 10-45 ±3.4 x 1038
c. character
d. boolean 2. double Size: 8 bytes
Range: ±4.9 x 10-324 ±1.8 x 10308
Categories:
a. integer
b. floating
c. character char Size: 2 bytes
Range: \u0000 \uFFFF
d. boolean
Categories:
a. integer
b. floating
c. character
d. boolean Size: 1 byte
boolean Range: true | false
• The operators for numeric data types include the standard arithmetic
operators: addition (+), subtraction (–), multiplication (*), division (/), and
remainder (%).
• Integer Literals:
• 100.2f or 100.2F
• 100.2d or 100.2D.
• Scientific Notation
• Casting is an operation that converts a value of one data type into a value of
another data type.
System.out.println((int)1.7);
System.out.println((double)1 / 2);
System.out.println(1 / 2);
double d = 4.5;
int i = (int)d;
• The problem is to write a program that computes loan payments. The program
lets the user enter the interest rate, number of years, and loan amount, and
displays the monthly and total payments.
• The pow(a, b) method in the Math class can be used to compute ab.
1. Prompt the user to enter the annual interest rate, number of years, and
loan amount.
2. Obtain the monthly interest rate from the annual interest rate.
char ch = (char)0XAB0041;
// the lower 16 bits hex code 0041 is assigned to ch
System.out.println(ch); // ch is character A
• When a floating-point value is cast into a char, the floating-point value is first cast into an int, which is then
cast into a char.
• The Unicode of 'a' is 97, which is within the range of a byte, these implicit
castings are fine: byte b = 'a';
• But the following casting is incorrect, because the Unicode \uFFF4 cannot fit
into a byte: byte b = '\uFFF4';
• Suppose you want to develop a program that classifies a given amount of money
into smaller monetary units. The program lets the user enter an amount as a
double value representing a total in dollars and cents, and outputs a report
listing the monetary equivalent in dollars, quarters, dimes, nickels, and pennies.
• String is actually a predefined class in the Java library just like the classes
System, and Scanner.
• A program can compile and run properly even if written on only one line,
but writing it all on one line would be bad programming style because it
would be hard to read.
• Use javadoc comments (/** ... */) for commenting on an entire class or an
entire method.
• If a name consists of several words, concatenate them into one, making the
first word lowercase and capitalizing the first letter of each subsequent word:
y = 4(x-3) + 20
Viết chương trình nhập x từ bàn phím và tính giá trị của y, với x và y là kiểu
số nguyên.
4. Ex4: Nhập vào bán kính hình tròn. Tính chu vi và diện tích hình tròn.
5. Ex5: Nhập vào tổng số giây. Hãy chuyển đổi sang giờ, phút, giây và in ra
theo dạng h:m:s.
Ví dụ: 19999 giây => 5:3:19
7. Nhập vào các số thực xA, yA, xB, yB là hoành độ và tung độ của 2
điểm A, B. Tính khoảng cách d giữa 2 điểm theo công thức d =
sqrt((xA-xB)2 + (yA-yB)2)
• Core Java Volume I Fundamentals, 8th, Cay S.Horstmann & Gary Cornell