Java Programming Fundamentals: Statements, Variables, Expressions
Learn about Java programming fundamentals including statements, variables, expressions, literals, and blocks. Understand the essentials of Java programming such as variable declaration, comments, and identifier naming conventions. Explore the basic building blocks of Java programs through practical examples and clear explanations.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
L ecture C ontents Statements Comments, Variables, Expressions, Literals and Blocks
Fundamentals of Java Program Java Statements Astatement in Java forms a complete command to be executed and can include one or more expressions. Statements in Java can be one of the following: Comments Variable Declaration/Initialization Control statement (if, switch, while, for, method invocation) Expressions Block of statements
Java Comments Java supports three types of comments Single Line comment Multiline comment Documentation comment The general forms are as follows: // Single line comment. // the single line is a type of comment in Java. /* Multiline Comment. These comments can span multiple lines. The compiler ignores all text up until */ /** Javadoc comment. The compiler ignores this text too. However, the javadoc tool looks for these comments and interprets tags for documentation generation purposes. These are used to generate documentation or reference for java application/software. javadoc tool recognizes these tags. @author XYZ @version 14.0.1 */
Variables Variables are a symbolic name given to a memory location. Variables may be of type object or simple variables. Like other compiled languages, variables must be declared before they can be used as Java is statically typed language. All variables have a type which is enforced by the compiler. Variables in Java are Case sensitive The general form of a variable declaration is: type variable-name [= value][,variable-name[= value]]; Examples: int total; float interest = 6.0; boolean isFinished; String name;
Variables/Identifier Name Identifiers are the name given to the variables, classes or methods. Java has a series of rules which define valid variable names and identifiers. Identifiers can contain letters, numbers, the underscore (_) character and the dollar sign character($) Identifiers must start with a letter, underscore or dollar sign. Identifiers are case sensitive. It means upper and lowercase letters in Java program matters. i.e. myname and myName both are different in Java program even though both identifiers have same characters and order of characters. Identifiers cannot be the same as reserved Java keywords. Naming convention for variables in the Java. When creating variables, choose a name that makes sense or meaningful name to the variable expected. E.g. totalAmount If you choose one-word variable names, use all lowercase letters. E.g. name If you choose variable names having more than one word, use all lowercase letters for the first word and capitalize the first letter of each subsequent word. e.g. firstName Valid Indentifier Names Invalid Indentifier Names myName _myName total _total total5 ___total5 total5$ $total36_51$ 1myName total# default My-Name
Variable Declaration The syntax for the declaration of a variable is: datatype variable_name; data type may be one of the simple types or may be the name of a class. variable _name is a legal Java identifier; the rules for simple variable identifiers are the same as those for object identifiers For example: int myAge; double cashTotal; We can also declare multiple variables of the same type using a single instruction; for example: int x, y, z; // int means integer
Variable Initialization Assigning the value to the variable is known as variable initialization. A variable can be initialized both during declaration and during the later stages of the program as required. data_type variable_name = value; or data_type variable_name; //initialization at declaration time //variable declaration variable_name = value; // variable initialization For example: int myAge=30; int myAge=30, myWeight=75; //initialization at declaration time //initialization at declaration of more than one variables double cashTotal; cashTotal=10000; //variable declaration // variable initialization
Keywords/ Reserved words in Java Keywords are predefined, reserved words used in program that have special meanings to the compiler. We can not use these reserved words as variable or class or method name. Keywords in Java are case sensitive and all keywords in lower case. abstract final native private protected public static synchronized transient volatile break case catch continue default do else finally for if return switch throw try while class extends implements interface throws boolean byte char short int long float double void import package instanceof new super this false null true
Java Literals A literal is nothing but a value. A literal is a notation for representing a fixed value in source code. Values for variables like 1, 4.5, true, '\u0050' that appear directly in a program without requiring computation are literals. E.g. boolean result=true; In the above example, boolean is datatype , result is a variable name and true is a value stored to the the variable result . We found integer, floating point, character and string literals in Java.
Java Expressions A Java expression consists of variables, operators, literals, and method calls. An expression evaluates to the value. Examples: int score; score = 90; //score=90 is an expression An expression is part of the Java statement. int a=5+5; We have a statement, and 5+5 is an expression which is present in the statement as the part of statement. int a=10, b=20, c; c=a+b; // a+b is an expression if(a==b) // a==b is an expression { }
Java Blocks A Java block is group of statements (zero or more) that are enclosed within the curly opening and closing bracket {}. Examples: class class_name { //class body } return datatype methodname(){ //start of block //method body } //start of block //end of block //end of block if(expression){ //empty block with no statement } //start of block //end of block