CC2 Week-2
CC2 Week-2
CC2 Week-2
In Java, all code must reside inside a class. By cconvention, the name of the main class
should match the name of the file that holds the program.
You should also make sure that the capitalization of the filename matches the class name.
Object - Objects are characterized by two components namely, methods and attributes or
variables. Any object is nothing but an instance of a class.
Instance Variables - each object has its set of variables. An object's state is made by the
qualities allotted to these variables during program execution.
Methods - A method is the definition of a method. Moreover, a class can contain numerous
methods. It is in these methods that behaviors like where the rationaes are composed,
information is controlled and all the activities are executed.
Arguments - are pieves of information that are sent into, or passed to, a method, usually
because the method requires the information to perform its task or carry out its purpose.
import javax.swing.*;
3 /*
* @author InFeliouS
*/
7 System.out.println("Hello World!");
3. Multi-line comments start with /* and ends with */ . Any text between /* and */ will
be ignored by Java (inshort, comment).
4. DemoWeek2 is an identifier that is the name of the class (user-defined) "This should be in a
file named DemoWeek2"
6. static means this method works withour instantiationg an object of the class.
8 }
9 }
1. The public keyword is an access modifier, which allows the programmer to control the
visibility of class members.
2. The keyword static allows main() to be called without having to instantiate a particular
instance of the class.
3. The keyword void simply tells the compiler that main() does not return a value.
4. The last character on the line is the { . This signals the start of main() 's body. All of the
code that comprises a method will occur between the method's { and its } .
5. System is a predefined class that provides access to the system, and out is the output
stream that is connected to the console.
7. "Hello World!" is a literal string that is the argument to the println() method.
System.out.println(459);
Every time an application containing the constant 459 is executed, the value 459 is
displayed.
A variable is a name memory location that you can use to store value.It can only hold one
value at a time, but the value it can holds can change.
byte
byte x = 100;
byte y = -47;
short
short x = 25949;
short y = -1294;
int
Main datatype to be used when dealing with numerical data w/o decimals.
int x = 5341312;
int y = -95739;
long
Max = 9 223 372 036 834 775 807 ; Min = - 9 223 372 036 834 775 808 (inclusive)
This sort is utilized when a more extensive memory range than int is required.
long x = 7817423190672;
long y = - 192847359218523;
float
A dataype which is known 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 numbers
float x = 51.125f;
double
A dataype which is known for its solitary exactness, 64-bit IEEE 754 gliding point
double is for the most part utulized as the default information sort for deciaml
qualities.
double x = 891453.52314;
boolean
Any boolean variable can assume one of the two values: true or false .
char
- 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. (Basically means Hi and hi are different from each
other, both of them may be used as variables seperately)
All identifiers ought to start with a letter (beginning to end or a to z), underscore _ or
specia character $ .
After the first character, identifiers can have any mix of characters.
Most significantly, identifiers are case-sensitive (Sample is not the same with sample).
+ - Addition (Sum)
- - Subtraction (Difference)
* - Multiplication (Product)
/ - Division (Remainder)
% - Modulo (Remainder)
++ - Increment (increases the value of a variable by 1)
package demoweek2;
/*
* @author InFeliouS
*/
System.out.println("Hello World!");
System.out.println("Hello Pogi");
Output:
Hello World!
Hello Pogi!
/*
* @author InFeliouS
*/
System.out.print("Hello World!");
System.out.print("Hello Pogi");
Output:
package demoweek2;
/*
* @author InFeliouS
*/
newMethod();
/*
*/
Output:
Hi BSIT 1-4, this is a test print
Note: Hindi ko na sinama yung "Variable Declaration" dahil may notes na about sa datatypes
and variables.
useBasicOperator();
int num1,num2,result;
num1 = 10;
num2 = 25
System.out.println(result);
Output:
35
- Concatenation
Concatenation is the process of combining two or more strings to form a new string by
subsequently appending the next string to the end of the previous strings.
public class DemoWeek2 {
System.out.println(firstName + lastName);
System.out.println(firstName.concat(lastName));
Output:
Infelious Rhaplanca
Infelious Rhaplanca
Import java.util.Scanner;
//Setting up Input
FirstName = input.nextLine();
LastName = input.nextLine();
Output:
InFelious
Rhaplanca