Week2 Intro To Java 2
Week2 Intro To Java 2
Fundamentals of
Computer Programming
Program Source
WRITE Code Bytecode
COMPILE RUN
(SOURCE CODE)
The programmer write Java compiler takes java JVM translate bytecode
the code using Java as program as input and into machine language
programming language. generate java bytecode and executes each
as output. program statement as
soon as it is translated.
JAVA BYTECODE
Bytecode refers to a low-level representation of code. It is an
intermediate representation of code that is generated by a
compiler from the source code written by a programmer. It is
not directly executed by the computer's hardware or
operating system, but rather by a virtual machine or
interpreter that understands the bytecode instructions.
SUMMARY:
In summary, the JRE provides the runtime environment to execute
Java applications, the JVM executes Java bytecode and manages
runtime operations, the JDK is used for developing Java applications
and includes the compiler, the compiler translates human-readable
Java source code into bytecode, and the JVM (partly) acts as an
interpreter to execute bytecode efficiently.
SYNTAX ERROR VS LOGICAL ERROR
A syntax error occurs when the code violates the rules of the programming language's
syntax. These rules dictate how statements and expressions should be structured,
including proper placement of brackets, parentheses, semicolons, keywords, and other
language-specific elements.
A logical error occurs when the program's code is syntactically correct but does not
produce the expected output due to incorrect logic or flawed reasoning in the
program's design. Logical errors stem from incorrect algorithmic or logical decisions
made by the programmer.
DEBUGGING
Debugging is the process of finding and fixing mistakes in computer
programs. These mistakes, or "bugs," can cause problems like crashes,
wrong results, or unexpected behavior. Errors can occur due to
various reasons, such as syntax mistakes, logical flaws, incorrect data,
unexpected behavior, or performance issues. Debugging is an
essential skill for programmers to ensure the correctness and
reliability of their code.
Integrated Development Environment
• The answer is we use an IDE (Integrated Development Environment) to
write code
• A place to write, run and debug code and also convert it to machine code
• IDE are like any other program on your computer except used for the
facilitation of code.
• Examples: Netbeans, Eclipse, IntelliJ, visual studios, sublime etc.
• Built in Error-checking
• Auto fill for frequently used words
Remember that computers can only understand and perform the
manipulation of 0’s and 1’s. These 0’s and 1’s are called binary digits.
The smallest unit of information that a computer supports refers to
bits. For the user to communicate with computers, these digits are
being combined to represent a character, number or symbol. As
programmers, it is essential to understand how data is being
translated from one form to another.
BIT vs BYTE
The term "bit" stands for "binary digit." A bit is the smallest unit of data in a
computer and can represent one of two states: 0 or 1. It's like the atom of
computing. Computers process and store information using bits. For instance, a bit
can represent the state of a switch being on (1) or off (0).
A "byte" is a group of 8 bits. It's a fundamental unit of data that's often used to
represent a character, like a letter, number, or symbol. Bytes provide a way to
work with larger pieces of information than just individual bits. A byte can
represent 256 different values (2^8), which includes all the possible combinations
of 8 bits.
NUMBER SYSTEM
NUMBER SYSTEM
This is an example of
a line comment.
COMMENTS IN JAVA
There are three types of comments in Java:
• Block comments start with a forward slash and an asterisk ( /* ) and end
with an asterisk and a forward slash ( */ ). A block comment can appear on
a line by itself, on a line before executable code, or on a line after
executable code. Block comments also can extend across as many lines as
needed.
COMMENTS IN JAVA
• Block comments start with a forward slash and an asterisk ( /* ) and end
with an asterisk and a forward slash ( */ ). Shortcut (bcom + tab)
COMMENTS IN JAVA
There are three types of comments in Java:
• Javadoc comments are a special case of block comments. They begin with
a forward slash and two asterisks ( /** ) and end with an asterisk and a
forward slash ( */ ). You can use javadoc comments to generate
documentation with a program named javadoc.
COMMENTS IN JAVA
• Javadoc comments are a special case of block comments. Shortcut (/** + enter)
This is an example of
a javadoc comment.
Commenting out
If a program is not performing as expected, you can “comment out”
various statements and subsequently run the program to observe the
effect. When you comment out a statement, you turn it into a
comment so the compiler does not translate it, and the JVM does not
execute its command. This can help you pinpoint the location of errant
statements in malfunctioning programs.
COMMENTING OUT IN JAVA
• You can use a single line comment to comment out a statement.
Dots separate
println() is a method. Method
classes, objects,
Closing curly braces of names are always followed by "Hello World!" is a literal string
and methods.
method main. parentheses. that is the argument to the
println() method.
In Java, you can create a method within a class to define a specific set
of actions or behaviors that the class can perform. Methods are
blocks of code enclosed within curly braces ({}) and can optionally
take parameters and return values.
CREATING A NEW METHOD:
ACCESS NON-ACCESS RETURN TYPE: IDENTIFIER:
MODIFIER: MODIFIER: void Method name
public static
METHOD
BODY:
This is where
you’ll write
your code
OPENING CURLY
PARAMETER: BRACES
()
{
CLOSING CURLY THEN HIT ENTER
BRACES
}
METHOD INVOCATION
In Java, method invocation refers to the process of calling a method on an
object or a class. Methods are blocks of code that perform a specific task,
and they can be either instance methods / non-static method (associated
with an object) or static methods (associated with a class). Here's how
method invocation works in Java:
METHOD INVOCATION
Go to
Editor
and then
choose
Code
Templates
CREATING A CUSTOM SHORTCUT
Click new
and then
type the
“shortcut”
you want
then click
OK.
CREATING A CUSTOM SHORTCUT
Type the
following
code in
the
expanded
Text:
CREATING A CUSTOM SHORTCUT
You can choose
the combination
of your shortcut
here, then click
OK
IDENTIFIERS,
VARIABLES, AND DATA TYPES
IDENTIFIERS
▪ Identifier - In programming languages, identifiers are used for identification purposes.
▪ Case-sensitivity - Java is case sensitive, which implies that the identifier Hi and hi
would have distinctive importance in Java.
▪ Java Identifier- All Java components require names. Names utilized for classes,
variables and strategies are called identifiers. In Java, there are a few focuses to recall
about identifiers.
long:
• long information sort is a 64-bit marked two’s supplement whole number.
• Maximum value for this data type is 2^63 -1, which is equal to
9,223,372,036,854,775,807.
• Minimum value for this data type is -2^63, which is equal to -9,223,372,036,854,775,808.
This sort is utilized when a more extensive memory range than int is required.
• The default value for those data type is 0l.
• Example: long x = 174636l, int y = -536452l
PRIMITIVE DATA TYPES
float:
• float is a data type, which is know for its solitary exactness, 32-bit IEEE 754 gliding
point.
• float is for the most part used to spare memory in vast exhibits of coasting point
numbers.
• The default value for this data type is 0.0f. float information sort is never utilized
for exact values, for example, money.
• Example: float x = 254.3f
double:
• double information sort is a float with two fold exactness 64-bit IEEE 754 drifting
point.
• This information sort is for the most part utilized as the default information sort for
decimal qualities.
• double information sort ought to never be utilized for exact values, for example,
money.
• The default value for this data type is 0.0d.
• Example: double x = 321.4
PRIMITIVE DATA TYPES
boolean:
• boolean information sort speaks to one bit of data.
• Any boolean variable can assume one of the two values: true or false.
• This information sort is utilized for basic banners that track genuine/false
conditions.
• The default value for this data type is false.
• Example: boolean check = true;
char:
• char information sort is a solitary 16-bit Unicode character.
• Maximum value for a variable of this type is “\uffff” (or 65,535
comprehensive).
• Minimum value for a variable of this type is “\u0000” (or 0).
• char information sort is utilized to store any character.
• example: char text =‘a’
Reference Data Types (Reference Types):
In Java, a reference data type refers to a type that represents
references to objects in memory rather than holding the actual
data directly. These types allow you to work with more complex
data structures and objects. Unlike primitive types that store the
actual value, reference types store a reference or memory
address pointing to the location where the object's data is stored.
REFERENCE DATA TYPES
Reference data types are used to store references to objects, rather
than the actual data.
• Primitive data types store the actual data value directly in the variable.
• Reference data types store a reference (memory address) to where the data
is located in memory. This allows reference types to represent more complex
data structures and share data efficiently, but it adds a layer of indirection
compared to primitive types.
VARIABLE DECLARATION
Variables are containers for storing data values. In a Java program, you must
declare a variable before it can be used. A variable declaration has the
following form:
datatype identifier;
datatype identifier1, identifier2, identifier3;
ASSIGNMENT STATEMENTS
An assignment statement that
has a variable of a primitive
type on the left side of the
equal sign causes the
following action: First, the
expression on the right side of
the equal sign is evaluated,
and then the variable on the
left side of the equal sign is
set to this value.
INITIALIZE VARIABLES
A variable that has been declared, but that has not yet been
given a value by an assignment statement (or in some other
way), is said to be uninitialized. If the variable is a variable of a
class type, it literally has no value. If the variable has a primitive
type, it likely has some default value. However, your program
will be clearer if you explicitly give the variable a value, even if
you are simply reassigning the default value.
Combining a Variable Declaration
and an Assignment
You can combine the declaration of a variable with an assignment
statement that gives the variable a value.
SYNTAX
datatype identifier = value;
datatype identifier1 = value, identifier2 = value, identifier3 = value;
EXAMPLES
int numberSeen = 0, increment = 5;
double height = 12.34, prize = 7.3 + increment;
char answer = 'y';
Combining a Variable Declaration
and an Assignment
CONCATENATION ON STRINGS
You can connect—or join or paste—two strings together to obtain a larger string. This
operation is called concatenation and is performed by using the + operator. When this
operator is used with strings, it is sometimes called the concatenation operator.
DISPLAYING DATA
println() Method:
• The println() method is used to display text or data on the console and then move the
cursor to the beginning of the next line.
• It adds a newline character (\n) after the printed content, so the next output will
appear on a new line.
• println() is typically used when you want to separate lines of output or start a new
line for the next piece of information.
print() Method:
• The print() method is used to display text or data on the console without moving the
cursor to a new line.
• It prints the output and leaves the cursor at the end of the printed content on the
same line.
• If you use print() multiple times consecutively, the output will appear on the same
line without any space or line break in between.
SIMPLE INPUT USING
SCANNER
SIMPLE INPUT
We use the class Scanner, which Java supplies, to accept keyboard input. Our
program must import the definition of the Scanner class from the package
java.util. Thus, we begin the program with the following statement:
import java.util.Scanner;
The following line sets things up so that data can be entered from the keyboard:
Scanner scanner = new Scanner(System.in);
This line must appear before the first statement that takes input from the
keyboard.
int age = scanner.nextInt();
This assignment statement gives a value to the variable age. The expression on
the right side of the equal sign.
SCANNER CLASS METHODS
Method Description
nextDouble() Retrieves input as a double
nextInt() Retrieves input as an int
nextLine() Retrieves the next line of data and returns it as
a String
Next() Retrieves the next complete token as a String
ECHOING INPUT
Repeating as output what a user has entered as input is called echoing the input.
Echoing input is a good programming practice; it helps eliminate
misunderstandings when the user can visually confirm what was entered.
ECHOING INPUT
ECHOING INPUT
Pitfall: Using nextLine() Following One of the Other Scanner Input Methods
You can encounter a problem when you use one of the numeric Scanner class retrieval
methods or the next() method before you use the nextLine() method.
Don’t do it:
If you use any other Scanner
input methods prior to string
input that uses nextLine(),
the string input is ignored
unless you take special
action.
Pitfall: Using nextLine() Following One of the Other Scanner Input Methods
The solution to the problem is simple. After any next(), nextInt(), or nextDouble() call,
you can add an extra nextLine() method call that will retrieve the abandoned Enter key
character. Then, no matter what type of input follows, the program will execute
smoothly.
This statement
consumes the Enter key
that follows the integer.
Fundamentals of Computer Programming
The End