Chapter 1 - Introduction To Computers, Programs, and Java
Chapter 1 - Introduction To Computers, Programs, and Java
Example:
• class Employee
• class Student
• class AddTwoNumber
Variables naming conventions
The variable name should start with a lowercase letter. Parameter names,
member variable names, and local variable names should be written
in lowerCamelCase.
Example:
• firstName
• lastName
• sum
Comment
• comment that documents what the program is and how it is
constructed. Comments help programmers to communicate and
understand the program.
• They are not programming statements and thus are ignored by the
compiler.
• Line comment:- In Java, comments are preceded by two slashes (//)
on a line, called a line comment.
• Block comment:- enclosed between /* and */ on one or several
lines, called a block comment or paragraph comment.
• When the compiler sees //, it ignores all text after // on the same line.
When it sees /*, it scans for the next */ and ignores any text
between /* and */.
• Here are examples of comments:
System.out.println("Welcome to Java!");
}
Special Characters
Character Name Description
3 System.out.println("Programming is fun!");
4 System.out.println("Fundamentals First");
5 System.out.println("Problem Driven");
6 }
7 }
• Further, you can perform mathematical computations and display the result on
the console.
• gives an example of evaluating
ComputeExpression.java
1 public class ComputeExpression {
2 public static void main(String[] args) {
3 System.out.println((10.5 + 2 * 3) / (45 – 3.5));
4 }
5 }
OutPut:
0.39759036144578314
System.out.println
• Two ways to use System.out.println :
• System.out.println("text");
Prints the given message as output.
• System.out.println();
Prints a blank line of output.
Programming Errors
• Programming errors can be categorized into three types: syntax errors, runtime
errors, and logic errors.
• Syntax Errors:- Errors that are detected by the compiler are called syntax errors
or compile errors. Syntax errors result from errors in code construction, such as
mistyping a keyword, omitting some necessary punctuation, or using an opening
brace without a corresponding closing brace. These errors are usually easy to
detect because the compiler tells you where they are and what caused them.
Syntax
• syntax: The set of legal structures and commands that can be
used in a particular language.
• The “spelling” and “grammar” of a programming language.
• Every basic Java statement ends with a semicolon ;
• The contents of a class or method occur between { and }
■ The string Welcome to Java should be closed with a closing quotation mark
in line 3.
Syntax error example
1 public class Hello {
2 pooblic static void main(String[] args) {
3 System.owt.println("Hello, world!")_
4 }
5 }
• Compiler output:
Hello.java:2: <identifier> expected
pooblic static void main(String[] args) {
^
Hello.java:3: ';' expected
}
^
2 errors
• The compiler shows the line number where it found the error.
More on syntax errors
• Java is case-sensitive
• Hello and hello are not the same
• Another example of runtime errors is division by zero. This happens when the
divisor is zero for integer divisions.
ShowRuntimeErrors.java
1 public class ShowRuntimeErrors {
3 System.out.println(1 / 0);
4 }
5 }
} Type this closing brace right away to match the opening brace
Common Error 2: Missing Semicolons
System.out.println("Programming is fun!");
System.out.println("Fundamentals First");
System.out.println("Programming is fun!");
System.out.println("Fundamentals First");
System.out.println("Problem Driven );
}
4 }
5 }