0% found this document useful (0 votes)
11 views

Week2 Intro To Java 2

The document discusses fundamentals of computer programming in Java including key Java buzzwords like object-oriented and portable, the Java programming process of writing source code that gets compiled to bytecode and run by the JVM, common number systems like binary and hexadecimal used in computing, and basics of Java syntax like comments, the main method, and commenting out code.

Uploaded by

jiwonhyung0234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Week2 Intro To Java 2

The document discusses fundamentals of computer programming in Java including key Java buzzwords like object-oriented and portable, the Java programming process of writing source code that gets compiled to bytecode and run by the JVM, common number systems like binary and hexadecimal used in computing, and basics of Java syntax like comments, the main method, and commenting out code.

Uploaded by

jiwonhyung0234
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 78

CC102 Fundamentals of Computer Programming

Fundamentals of
Computer Programming

XENIORITA ALONDRA BIO WEEK 2-3


Programming
Language
The Java Buzzwords
Although the fundamental forces that necessitated the invention of Java are portability and
security, other factors also played an important role in molding the final form of the language. The
key considerations were summed up by the Java team in the following list of buzzwords:
• Simple • Multithreaded
• Secure • Architecture-neutral
• Portable • Interpreted
• Object-oriented • High performance
• Robust • Dynamic
• Distributed
The program
development process
PHASES OF 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

Number system is also called numeral system. Number system is


any notation that represents numerals or numbers. There are a
few common number systems that people use:
BINARY SYSTEM
This is considered as the computer machine language. It means that on the
machine level, this is only recognizable data to computer. The binary simulates the
toggle switches that a computer uses to perform its calculations. It has the base or
radix of 2.
The binary system uses two digits (0 and 1) to represents all quantities, each digits
represents a power of 2.
DECIMAL SYSTEM
This is the most common number system we have ever known and the basis of
how we count, as we know it. Decimal number system has a base or radix of 10.
This means that the numbers are notated with one digit starting a sequence from
1 to 9, ending the sequence with a 0 prefixed with another digit, and then start
another sequence from 1-9.
The decimal system uses ten (10) digits, 0-9 to represent all quantities and each
digit represents a power of ten.
OCTAL SYSTEM
The octal number system is a result of simplifying the representation of
characters in programming a computer. As computing became more and more
advanced, as well as convenient for users, programmers needed to find a way to
simplify the way the data is represented. So they group the binary bits into three
(3) called octets. Thus, 23 is equal to 8, deriving the name octal.
The octal system uses eight digits (0-7) to represents all quantities, each digits
represents a power of 8.
HEXADECIMAL SYSTEM
This number system is a spin off from the octal system. Due to significant
developments in the way computers are being used, the instructions and character
representations into it have grown as well. The hexadecimal system has a base or
radix of 16.
The hexadecimal system uses 16 digits (0-9 and A-F) to represents all quantities,
each digits represents a power of 16.
ASCII TABLE
(American Standard Code for Information Interchange)

As the computer evolved to be an-around computing device, the need to input


and represent different characters must be made. Thus, a system has to be
introduced to translate characters to binary digits. The idea was to assign a
specific decimal number for each character(numbers, letters, punctuation and
other special character) converting it into String of bits. This String of bits is now
what we call a byte, consisting of 8 binary bits that could represents 255
characters. (A-Z is from 65-90, a-z is from 97-122)
ASCII TABLE
Anatomy of a Java
Basic Syntax
Understanding main method
Adding a Comment
Documenting your program code helps you remember why you wrote lines
of code the way you did. Program comments are nonexecuting statements
that you add to a program for the purpose of documentation. Programmers
use comments to leave notes for themselves and for others who might read
their programs in the future.
COMMENTS IN JAVA
There are three types of comments in Java:
• Line comments start with two forward slashes ( // ) and continue to the
end of the current line. A line comment can appear on a line by itself or at
the end (and to the right) of a line following executable code. Line
comments do not require an ending symbol.
COMMENTS IN JAVA
• Line comments start with two forward slashes ( // )

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.

*This will temporarily


disable a piece of code
without actually removing
it from your program
COMMENTING OUT IN JAVA
• You can also comment out multiple lines at once using block comment:

Opens with /* and close with */


To revert the code back to its
original state (uncomment it)
remove the comment symbols.
COMMENTING OUT IN JAVA
• You can also comment out multiple lines at once using single comment:

Use the shortcut


ctrl + / to comment out the
code and to revert the code
back to its original state
(uncomment it).
main() method
main( ) is simply a starting place for your program. It is the method called
when a Java application begins. A complex program will have dozens of
classes, only one of which will need to have a main( ) method to get
things started. Furthermore, for some types of programs, you won’t
need main( ) at all. However, for most of the programs main( ) is
required.
main() method
Keep in mind that Java is case-sensitive. Thus, Main is different from main. It
is important to understand that the Java compiler will compile classes that
do not contain a main( ) method. But java has no way to run these classes.
So, if you had typed Main instead of main, the compiler would still compile
your program. However, java would report an error because it would be
unable to find the main( ) method.
Understanding main method
The keyword static allows main( ) to be The keyword void simply tells
The public keyword is an access called without having to instantiate a the compiler that main( ) does
modifier, which allows the particular instance of the class. not return a value.
programmer to control the visibility
of class members. The last character on the line
is the {. This signals the start
System is a of main( )’s body. All of the
predefined class code that comprises a
that provides method will occur between
access to the the method’s opening curly
system, and out is brace and its closing curly
the output stream brace.
that is connected
to the console.

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.

Closing curly braces of class


DemoWeek2.
CREATING A 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

This is how you can


call a method.
methodName();
You can also try the
shortcut (ctrl + space)
then double click your
method name.
CREATING A CUSTOM SHORTCUT

Go to Tools and then


choose Options
CREATING A CUSTOM SHORTCUT

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.

➢ All identifiers ought to start with a letter (beginning to end or a to z),


underscore (_) or special character ($).
➢ After the first character, identifiers can have any mix of characters.
➢ You cannot use a keyword as an identifier.
➢ Most significantly, identifiers are case-sensitive. So, Sample is not same as
sample. Examples of identifiers include $salary, age, __1_value and _value.
➢ Examples of illicit identifiers include –compensation and 123abc.
IDENTIFIERS
JAVA KEYWORDS
IDENTIFIERS
Beginning an identifier with a lowercase letter and capitalizing subsequent
words within the identifier is a style known as camel casing. An identifier such
as lastName resembles a camel because of the uppercase “hump” in the middle.
VARIABLES
Variables in a program are used to store data such as numbers
and letters. They can be thought of as containers of a sort. The
number, letter, or other data item in a variable is called its value.
This value can be changed, so that at one time the variable
contains, say, 6, and at another time, after the program has run
for a while, the variable contains a different value, such as 4.
Primitive Data Types:
Primitive data types are the basic, fundamental types that Java
uses to represent simple values. Unlike reference data types,
which hold references to objects, primitive data types store the
actual values themselves. These types are used to represent
basic, atomic values like numbers and characters.
Primitive Data Types:
• Primitive data types are also known as "value types."
• They directly store the actual value of the data.
• Primitive data types are simple and small in size.
• When you assign a primitive data type to a variable, you're actually storing
the data itself in that variable.
• Examples of primitive data types include integers, floating-point numbers,
characters, and booleans.
PRIMITIVE DATA TYPES
An item’s data type describes the type of data that can be stored there, how much memory the item
occupies, and what types of operations can be performed on the data. Java provides for eight
primitive types of data. A primitive type is a simple data type.
byte:
• byte information sort is a 8-bit marked two’s supplement whole number.
• Maximum worth is 2^7 -1, which is equal to 127. This value is also included in the range of
these values.
• Minimum worth is -2^7, which is equal to -128.
• Default value stored in a variable of this type is 0. byte information sort is utilized to spare
space in vast exhibits, principally set up of numbers, since a byte is four times littler than an int.
• Example: byte x = 200, byte y = -20
short:
• short information sort is a 16-bit marked two’s supplement number.
• Maximum value is 2^15 -1, which is equal to 32,767. This number is also included in the range.
• Minimum value is -2^15, which is equal to -32,768. short information sort can likewise be
utilized to spare memory as byte information sort.
• A short is 2 times littler than an int. The default value for this data type is 0.
• Example: short x = 425164, short y = -76686
PRIMITIVE DATA TYPES
int:
• int information sort is a 32-bit marked two’s supplement number.
• Maximum value for this data type is 2^31 -1, which is equal to 2,147,483,647. This
number is also included in the range for this data type.
• Minimum value for this data type is -2^31, which is equal to - 2,147,483,648.
• int is for the most part utilized as the default information sort for its indispensable
qualities unless there is a worry about memory.
• The default value for this data type is 0.
• Example: int x = 826378, int y = -64782

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.

• Classes: User-defined data types that can have fields, methods,


and constructors.
• Interfaces: Define a contract for methods that implementing
classes must provide.
• Arrays: Homogeneous collections of elements.
• Enums: A special type used to define a set of constants.
• Others: References to instances of various built-in classes like
String, ArrayList, etc.
Reference Data Types (Reference Types):
• Reference data types are also known as "object types."
• They store references (memory addresses) to the location where the data is
stored in memory.
• Reference data types are more complex and can hold large amounts of data.
• When you assign a reference data type to a variable, you're storing a
reference to the data, not the data itself.
• Examples of reference data types include arrays, strings, and custom objects.
In summary, the key difference lies in how data is stored:

• 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

XENIORITA ALONDRA BIO WEEK 2-3

You might also like