Topic 01 : Introduction
Topic 02 : Identifiers & Variables
- Identifiers
- An identifier is a sequence of characters that consist of letters, digits, underscores (_), and dollar signs ($).
- An identifier must start with a letter, an underscore (_), or a dollar sign ($). It cannot start with a digit.
- An identifier cannot be a reserved word.
- An identifier cannot be true, false, or null.
- An identifier can be of any length.
- Declaring Variables
int x; // Declare x to be an integer variable; double radius; // Declare radius to be a double variable; char a; // Declare a to be a character variable;
- Assignment Variables
x = 1; // Assign 1 to x;
radius = 1.0; // Assign 1.0 to radius;
a = ‘A’; // Assign ‘A’ to a;
- Declaring and Initializing in One Step
int x = 1; // Declare x to be an integer variable // And assign 1 to x
double d = 1.4; // Declare d to be a double variable // And assign 1.4 to d
- Constants
final datatype CONSTANTNAME = VALUE;
final double PI = 3.14159;
final int SIZE = 3;
Topic 03 : Numbers
- Numerical Data Types
- Numeric Operators
- 5 / 2 yields an integer 2
- 5.0 / 2 yields an double value 2.5
- 5 % 2 yields 1 (the remainder of the division)
- Remainder Operator
Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd. Suppose today is Saturday and you and your friends are going to meet in 10 days. What day is in 10 days? You can find that day is Tuesday using the following expression:
Example : Displaying Time
Write a program that obtains hours and minutes from seconds.
public class DisplayTime {
public static void main(String[] args) {
int seconds = 10000;
int totalMinutes = seconds / 60;
int hours = totalMinutes / 60;
int minutes = totalMinutes % 60;
System.out.println( seconds + " seconds is " + hours + " hours and " + minutes + " minutes");
}
}
- NOTE
System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);
displays 0.5000000000000001, not 0.5, and
System.out.println(1.0 - 0.9);
displays 0.09999999999999998, not 0.1.
Integers are stored precisely. Therefore, calculations with integers yield a precise integer result.
- Number Literals
A literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are literals in the following statements:- int i = 34;
- long x = 1000000;
- double d = 5.0;
- Integer Literals
An integer literal can be assigned to an integer variable as long as it can fit into the variable. A compilation error would occur if the literal were too large for the variable to hold. For example, the statement byte b = 1000 would cause a compilation error, because 1000 cannot be stored in a variable of the byte type.
An integer literal is assumed to be of the int type, whose value is between -231 (-2147483648) to 231–1 (2147483647).
To denote an integer literal of the long type, append it with the letter L or l. L is preferred because l (lowercase L) can easily be confused with 1 (the digit one).
- Floating-Point Literals
Floating-point literals are written with a decimal point. By default, a floating-point literal is treated as a double type value. For example, 5.0 is considered a double value, not a float value.
You can make a number a float by appending the letter f or F, and make a number a double by appending the letter d or D. For example, you can use 100.2f or 100.2F for a float number, and 100.2d or 100.2D for a double number.
- Scientific Notation
Floating-point literals can also be specified in scientific notation, for example, 1.23456e+2, same as 1.23456e2, is equivalent to 123.456, and 1.23456e-2 is equivalent to 0.0123456. E (or e) represents an exponent and it can be either in lowercase or uppercase.
- Arithmetic Expressions
Example : Converting Temperatures
Write a program that converts a Fahrenheit degree to Celsius using
the formula: 
public class CLASSNAME {
public static void main(String[] args) {
// your statements…
System.out.println ( “RESULT MESSAGE“ );
}
}
Example : Converting Temperatures
public class FahrenheitToCelsius {
public static void main(String[] args) {
double fahrenheit = 100;
double celsius = (5.0 / 9) * (fahrenheit - 32);
System.out.println(“Fahrenheit " + fahrenheit + " is " + celsius + " in Celsius");
} }
- Shortcut Assignment Operators
- Increment and Decrement Operators
Using increment and decrement operators makes expressions short, but it also makes them complex and difficult to read. Avoid using these operators in expressions that modify multiple variables, or the same variable for multiple times such as this : int k = ++i + i.
- Numeric Type Conversion
When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules:
1. If one of the operands is double, the other is converted into double.
2. Otherwise, if one of the operands is float, the other is converted into float.
3. Otherwise, if one of the operands is long, the other is converted into long.
4. Otherwise, both operands are converted into int.
- Type Casting
Implicit Casting
double d = 3; ( Type Widening )Explicit Casting
int i = (int)3.0; (type narrowing)int i = (int)3.9; (Fraction part is truncated)
The blog gave us idea about variable,data types,operators and Type conversions etc my sincere thanks for sharing this valuable post
ReplyDeleteJava Training in Chennai
really you have posted an informative blog. it will be really helpful to many peoples. thank you for sharing such kind of an interesting articles.
ReplyDeletedotnet training in chennai
Hi admin..
ReplyDeleteyou have reelly good tallent in coading.you coading will clearly explain the control structures. It will help all the viewers to update their knowledge.Thanks for sharing.keep sharing more blogs.
Core Java Online Training
Hi, I am really happy to found such a helpful and fascinating post that is written in well manner.. Really very informative post you shared here. Keep sharing this type of informative blog. If anyone wants to become a Java professional learn Java Training in Bangalore. Nowadays Java has tons of job opportunities for all professionals...Big Data Hadoop Training in Bangalore | Data Science Training in Bangalore
ReplyDelete