While writing code, comments are important to consider because it is helpful for others to read your code and for yourself as well.
There are two different styles of Java comments: C style and C++ style. A C style comment begins with /* and ends with */. C style comments are used to document information that applies to the entire program or the block of code. For example:
/****************************************************************************
Due date: 1/1/2015
Author: Albert Einstein
Description: This program is to prompt user to enter student scores, calculate the average, and display the result to the console.
*****************************************************************************/
A C++ style comment begins with double slashes // and is followed one line of explanation. You must begin each line of a multi-line comment block with //. It is used to describe what a specific statement or group of the statements do. For example:
float sum, average; //declare float type of variables
//compute the average and display the result
average = sum / 5;
System.out.println(“Average = “ + average); //Concatenation using a "+" sign
Identifiers :
Identifiers are the names created by a programmer within a program. Variable names, class names, and method names are examples of identifiers. Identifiers cannot be any keywords reserved by the Java language. You must use the following rules in creating an identifier:
• An identifier starts with one of any letter, underscore _, or dollar sign $.
• It is followed by the combination of any letters, numbers (0-9), underscores, and dollar signs.
• It cannot be the same as any Java keyword.
The following are 53 Java keywords: (cannot be used by the programmer!)
abstract assert boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while
Primitive Data types:
Variable is a memory location where data can be stored. Java Provides 8 Different data types to store data.
Type Name Memory Requirement(in bytes) Value Range
byte integer in byte 1 -128 , -127
short short integer 2 -32768, -32767
int integer 4 -2,147,483,648 to -2,147,483,647
long integer in long 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807
float single float 4 -3.4E38 – 3.4E38 (up to 7 significant digits)
double double float 8 -1.7E308 – 1.7E308 (up to 16 significant digits)
char single character 2 Unicode character
boolean Boolean 1 true or false
Integer type variables, such as byte, short, int, and long, can only store whole numbers, while float and double variables store data as floating-point numbers. If a single-precision floating-point variable exceeds 7 significant digits, e.g., 123456.75, the eighth digit will be rounded up in the stored value. Java will display bigger or smaller floating-point numbers with scientific notation E. For example, 28600000.0 will be 2.86E7, and .000123 will be 1.23E-4.
NOTE : String is not a primary data type. It is provided by the Java language as an API (Application Programming Interface) class, or library class. We can tell String is a class as its name begins with an uppercase letter. Since String is a commonly used data type, Java provides a convenient way, direct referencing (referencing, in short) to define a String object. For example:
String obj = “I am a String”; //obj referencing to a String object
Variable Initialization:
Examples :
int a;
a = 1; //storing or assigning 1 to a
or int a = 1; //Single Statement
double value ;
value = 2.49;
or
double value =2.49 // Single Statement
boolean isValid = true ;
char letterB = 'B'; //Storing Letter B in the variable "letterB"
Operators:
2 + 3 // Arithmetic operation
In the above operation , "+" is a operator and 2, 3 are operands.
+ ---MEANING-----> adding of two operands
- ---MEANING-----> Subtraction of the right operand from the left operand
* ---MEANING------>Multiplication of two operands
/ ---MEANING------>Division of left operand from the right operand
% ---MEANING------->Reminder after division.
++ ---MEANING------->Increment
-- ---MEANING------> Decrement
+ ---MEANING ------> Positive Sign
- ----MEANING------> Negative Sign
There are 4 different ways to increase a variable by 1:
x = x + 1; //regular
x += 1; //short-cut
++x; // prefix incrementing
x++; // postfix incrementing
Concatenation:
Joining Strings are very easy in Java.
String firstName = "John",
String lastName = "Smith";
String fullName = firstName + " "+ lastName; //Joining Strings
Output : John Smith
Using Special Character in Java :
We must place a backslash “\” before a special character when it is used in a string. This notation is called an escape sequence. The first 3 escape sequences in the table control the cursor’s locations. The fourth one is used with a double quote.
Escape Sequence Example USE
\n String newline = "\nn"; Move cursor to beginning of
next line and display n
when string is displayed
\t String val = "u\tv"; Separate u and v with a tab
character in the output
\r String end = "none\r"; Position cursor at character "n"
when string is displayed.
\" String lang = "\"C++\" OOP"; Will display " C++" OOP
\\ String backSlash =" D:\\\\dir" ; Will display D:\\dir
There are two different styles of Java comments: C style and C++ style. A C style comment begins with /* and ends with */. C style comments are used to document information that applies to the entire program or the block of code. For example:
/****************************************************************************
Due date: 1/1/2015
Author: Albert Einstein
Description: This program is to prompt user to enter student scores, calculate the average, and display the result to the console.
*****************************************************************************/
A C++ style comment begins with double slashes // and is followed one line of explanation. You must begin each line of a multi-line comment block with //. It is used to describe what a specific statement or group of the statements do. For example:
float sum, average; //declare float type of variables
//compute the average and display the result
average = sum / 5;
System.out.println(“Average = “ + average); //Concatenation using a "+" sign
Identifiers :
Identifiers are the names created by a programmer within a program. Variable names, class names, and method names are examples of identifiers. Identifiers cannot be any keywords reserved by the Java language. You must use the following rules in creating an identifier:
• An identifier starts with one of any letter, underscore _, or dollar sign $.
• It is followed by the combination of any letters, numbers (0-9), underscores, and dollar signs.
• It cannot be the same as any Java keyword.
The following are 53 Java keywords: (cannot be used by the programmer!)
abstract assert boolean break byte case catch char class const continue default do double else enum extends false final finally float for goto if implements import instanceof int interface long native new null package private protected public return short static strictfp super switch synchronized this throw throws transient true try void volatile while
Primitive Data types:
Variable is a memory location where data can be stored. Java Provides 8 Different data types to store data.
Type Name Memory Requirement(in bytes) Value Range
byte integer in byte 1 -128 , -127
short short integer 2 -32768, -32767
int integer 4 -2,147,483,648 to -2,147,483,647
long integer in long 8 -9,223,372,036,854,775,808 9,223,372,036,854,775,807
float single float 4 -3.4E38 – 3.4E38 (up to 7 significant digits)
double double float 8 -1.7E308 – 1.7E308 (up to 16 significant digits)
char single character 2 Unicode character
boolean Boolean 1 true or false
Integer type variables, such as byte, short, int, and long, can only store whole numbers, while float and double variables store data as floating-point numbers. If a single-precision floating-point variable exceeds 7 significant digits, e.g., 123456.75, the eighth digit will be rounded up in the stored value. Java will display bigger or smaller floating-point numbers with scientific notation E. For example, 28600000.0 will be 2.86E7, and .000123 will be 1.23E-4.
NOTE : String is not a primary data type. It is provided by the Java language as an API (Application Programming Interface) class, or library class. We can tell String is a class as its name begins with an uppercase letter. Since String is a commonly used data type, Java provides a convenient way, direct referencing (referencing, in short) to define a String object. For example:
String obj = “I am a String”; //obj referencing to a String object
Variable Initialization:
Examples :
int a;
a = 1; //storing or assigning 1 to a
or int a = 1; //Single Statement
double value ;
value = 2.49;
or
double value =2.49 // Single Statement
boolean isValid = true ;
char letterB = 'B'; //Storing Letter B in the variable "letterB"
Operators:
2 + 3 // Arithmetic operation
In the above operation , "+" is a operator and 2, 3 are operands.
+ ---MEANING-----> adding of two operands
- ---MEANING-----> Subtraction of the right operand from the left operand
* ---MEANING------>Multiplication of two operands
/ ---MEANING------>Division of left operand from the right operand
% ---MEANING------->Reminder after division.
++ ---MEANING------->Increment
-- ---MEANING------> Decrement
+ ---MEANING ------> Positive Sign
- ----MEANING------> Negative Sign
There are 4 different ways to increase a variable by 1:
x = x + 1; //regular
x += 1; //short-cut
++x; // prefix incrementing
x++; // postfix incrementing
Concatenation:
Joining Strings are very easy in Java.
String firstName = "John",
String lastName = "Smith";
String fullName = firstName + " "+ lastName; //Joining Strings
Output : John Smith
Using Special Character in Java :
We must place a backslash “\” before a special character when it is used in a string. This notation is called an escape sequence. The first 3 escape sequences in the table control the cursor’s locations. The fourth one is used with a double quote.
Escape Sequence Example USE
\n String newline = "\nn"; Move cursor to beginning of
next line and display n
when string is displayed
\t String val = "u\tv"; Separate u and v with a tab
character in the output
\r String end = "none\r"; Position cursor at character "n"
when string is displayed.
\" String lang = "\"C++\" OOP"; Will display " C++" OOP
\\ String backSlash =" D:\\\\dir" ; Will display D:\\dir