Java Basics - Java Programming Tutorial. This chapter explains the basic syntaxes of the Java programming language. I shall assume that you could write some simple Java programs. They are ignored by the compiler and have no consequences to the program execution. Nevertheless, comments are VERY IMPORTANT for providing documentation and explanation for others to understand your programs (and also for yourself three days later).
There are two kinds of comments in Java: Multi- Line Comment: begins with a /* and ends with a */, and can span multiple lines. End- of- Line (Single- Line) Comment: begins with // and lasts till the end of the current line. I recommend that you use comments liberally to explain and document your codes. It performs a piece of programming action. A programming statement must be terminated by a semi- colon (; ), just like an English sentence ends with a period. This is because period crashes with decimal point - it is hard for the dumb computer to differentiate between period and decimal point in the early days of computing!)For examples.
Summer Programs; All Products. Wolfram Language Revolutionary knowledge-based programming language. Virtual Book > Mathematics and Algorithms > Manipulating Equations and Inequalities > Relational and Logical Operators. Java Programming Tutorial Java Basics. I shall assume that you could write some simple Java programs. Java provides four logical operators. Logical databases are special ABAP programs that retrieve data and make it available to application programs. You edit logical databases using Logical Database Builder in ABAP Workbench. Processing is a flexible software sketchbook and a language for learning how to code within the context of the visual arts. Java logical operators boolean a, b; Operation Meaning Note; a && b: logical AND.
Major logic programming language families include Prolog, Answer set. Transaction logic is an extension of logic programming with a logical theory of state. Programs with common sense Symposium on Mechanization of. Practice Online Java Programming Test and find out how much you score before you appear for your next. Logical Reasoning; Verbal. C# Programming; Java Programming; Have a question? Ask your question now!
System. out. println(. All the statements inside the block is treated as one single unit. Blocks are used as the body in constructs like class, method, if- else and for- loop, which may contain multiple statements but are treated as one unit (one body). There is no need to put a semi- colon after the closing brace to end a compound statement. Empty block (i. e., no statement inside the braces) is permitted. For examples. if (mark > = 5.
Java, like most of the computing languages, ignores extra white spaces. However, proper indentation (with tabs and blanks) and extra empty lines greatly improves the readability of the program. This is extremely important for others (and yourself three days later) to understand your programs. For example, the following one- line hello- world program works. But can you read and understand the program? A variable is used to store a piece of data for processing.
It is called variable because you can change the value stored. More precisely, a variable is a named storage location, that stores a value of a particular data type. In other words, a variable has a name, a type and stores a value. A variable has a name (aka identifier), e. The name is needed to uniquely identify each variable, so as to assign a value to the variable (e. A variable has a type. Examples of Java type are.
E- 6, having a optional decimal point and fractional part. String: meant for texts such as . Strings are enclosed within a pair of double quotes. A char is enclosed by a pair of single quotes. A variable can store a value of that particular data type. It is important to take note that a variable in most programming languages is associated with a type, and can only store value of the particular type.
For example, a int variable can store an integer value such as 1. NOT real number such as 1. The type determines the size and layout of the data, the range of its values, and the set of operations that can be applied. The following diagram illustrates three types of variables: int, double and String.
An int variable stores an integer (or whole number or fixed- point number); a double variable stores a floating- point number or real number; a String variable stores texts. Identifiers (or Names)An identifier is needed to name a variable (or any other entity such as a method or a class). Java imposes the following rules on identifiers: An identifier is a sequence of characters, of any length, comprising uppercase and lowercase letters (a- z, A- Z), digits (0- 9), underscore . Take note that blank and dash (- ) are not allowed, i. It cannot begin with a digit (0- 9) (because that could confuse with a number).
Identifiers begin with dollar sign ($) are reserved for system- generated entities. An identifier cannot be a reserved keyword or a reserved literal (e.
Identifiers are case- sensitive. A rose is NOT a Rose, and is NOT a ROSE. Caution: Programmers don't use blank character in names. It is either not supported (e. Java and C/C++), or will pose you many more challenges. Variable Naming Convention.
A variable name is a noun, or a noun phrase made up of several words with no spaces between words. The first word is in lowercase, while the remaining words are initial- capitalized. For example, thefont.
Size, room. Number, x. Max, y. Min, x. Top. Left and this. Is. AVery. Long. Variable.
Name. This convention is also known as camel- case. Recommendations. It is important to choose a name that is self- descriptive and closely reflects the meaning of the variable, e.
Of. Students or num. Students, but not n or x, to store the number of students. It is okay to use long names! Do not use meaningless names like a, b, c, d, i, j, k, i.
Avoid single- letter names like i, j, k, a, b, c, which is easier to type but often meaningless. Exception are common names like x, y, z for coordinates, i for index. Long names are harder to type, but self- document your program.
The act of declaring a variable allocates a storage (of size capable of holding a value of the type). Syntax. Exampletype identifier; type identifier. N; type identifier=initial. Value; type identifier. Value. 1, ..,identifier. N=init. Value. N. Number = 8. 8. String greeting.
Msg = . A variable is declared with a type. Once the type of a variable is declared, it can only store a value belonging to this particular type. For example, an int variable can hold only integer such as 1. NOT floating- point number such as - 2. The type of a variable cannot be changed inside the program, after it is declared.
A variable declaration statement begins with a type, and works for only that type. In other words, you cannot declare variables of two different type in a single declaration statement.
Constants (final Variables)Constants are non- modifiable variables, declared with keyword final. Their values cannot be changed during program execution. Also, constants must be initialized during declaration. For examples: final double PI = 3. Constant Naming Convention: Use uppercase words, joined with underscore. For example,1 + 2 * 3. Rate. principal * (1 + interest.
Rate) Assignment. An assignment statement: assigns a literal value (of the RHS) to a variable (of the LHS), e. RHS) and assign the resultant value to a variable (of the LHS), e. The syntax for assignment statement is: Syntax. Examplevariable=literal.
Value; variable=expression. The assignment statement should be interpreted this way: The expression on the right- hand- side (RHS) is first evaluated to produce a resultant value (called r- value or right- value). The r- value is then assigned to the variable on the left- hand- side (LHS) or l- value. Take note that you have to first evaluate the RHS, before assigning the resultant value to the LHS. For examples. number = 8. INVALID. number + 1 = sum; // INVALIDIn Java, the equal symbol '=' is known as the assignment operator.
The meaning of '=' in programming is different from Mathematics. It denotes assignment of the LHS value to the RHS variable, instead of equality of the RHS and LHS. The RHS shall be a literal value or an expression that evaluates to a value; while the LHS must be a variable. Note that x = x + 1 is valid (and often used) in programming. It evaluates x + 1 and assign the resultant value to the variable x. While x + y = 1 is allowed in Mathematics, it is invalid in programming (because the LHS of an assignment statement must be a variable).
Some programming languages use symbol . We shall describe the primitive types here and the reference types (classes and objects) in the later chapters on . Java has eight primitive types, as listed in the above table: There are four integer types: 8- bit byte, 1. They are signed integers in 2's complement representation, and can hold an integer value of the various ranges as shown in the table. There are two floating- point types: 3. IEEE 7. 54 standard. A float can represent a number between .
A double can represented a number between . Take note that not all real numbers can be represented by float and double.
This is because there are infinite real numbers even in a small range of say . Most of the floating- point values are approximated to their nearest representation.
The type char represents a single character, such as '0', 'A', 'a'. In Java, char is represented using 1. Unicode (in UCS- 2 format) to support internationalization (i. A char can be treated as a 1. For example, character '0' is 4. H (hexadecimal); character 'A' is 6. H (hexadecimal); character 'a' is 9.
H (hexadecimal). Java introduces a new binary type called . The maximum, minimum and bit- size of int are kept in constants INTERER.
MIN. String is not a primitive type, and will be further elaborated later. In Java, a char is enclosed by single quotes (e. A', '0'), while a String is enclosed by double quotes (e.
For example. String message = . Most of the times, the decision is intuitive.
For example, use an integer type for counting and whole number; a floating- point type for number with fractional part, String for text message, char for a single character, and boolean for binary outcomes. Rules of Thumb. Use int for integer and double for floating point numbers. Use byte, short, long and float only if you have a good reason to choose that specific precision. Use int for counting and indexing, NOT floating- point type (float or double).
This is because integer type are precise and more efficient in operations. Use an integer type if possible. Use a floating- point type only if the number contains a fractional part. Data Representation.
Read . In brief, It is important to take note that char '1' is different from int. String. They are represented differently in the computer memory, with different precision and interpretation.
For example, byte. There is a subtle difference between int. Furthermore, you MUST know the type of a value before you can interpret a value. For example, this value .
He has chosen service plan 'B' among plans 'A', 'B' and 'C', plus on- site servicing. Identify the data types and name the variables.