MODULE 3 3rd Reporting
MODULE 3 3rd Reporting
MODULE 3 3rd Reporting
LANGUAGES
FUNDAMENTALS
REPORTERS:
Jeezreel Mabolis
John Salre N. Largo
Cristian Jay Bueno
Marcelino Baldapan Jr.
Dave Aberte
3.1 Data Types and Naming Conventions in Java
Data Types in Java
* Used to represent the type of memory allocation.
* Used with variables and methods.
* These are fixed.
As the name suggests, data types specify the type of data that can be
stored inside variables in Java .
Primitive Data Types
Primitive data types are defined already by the programming
language. In Java there are 8 primitive data types.
Size Default Value Type of Value Stored
Here, speed is a variable, and the data type of the variable is int.
The int data type determines that the speed variable can only contain
Integers.
Note: In addition to primitive data types, there are also referenced types
(object type).
8 Primitive Data Types
1. boolean type
The boolean data type has two possible values, either true or false.
Default value: false.
They are usually used for true/false conditions.
Example 1: Java boolean data type
class Main {
public static void main(String[] args) {
The byte data type can have values from -128 to 127 (8-bit signed
Two's complement integer).
If it's certain that the value of a variable will be within -128 to 127,
then it is used instead of int to save memory.
Default value: 0
Example 2: Java byte data type
class Main {
public static void main(String[] args) {
byte range;
range = 124;
System.out.println(range); // prints 124
3. short type
* The short data type in Java can have values from -32768 to 32767
(16-bit signed two's complement integer).
* If it's certain that the value of a variable will be within -32768 and
32767, then it is used instead of other integer data types (int, long).
Default value: 0
Example 3: Java short data type
class Main {
public static void main(String[] args) {
short temperature;
temperature = -200;
System.out.println(temperature); // prints -200
4. int type
* The int data type can have values from -2 31 to 2 31 -1 (32-bit signed
Two's complement integer).
* If you are using Java 8 or later, you can use an unsigned 32-bit
integer. This will have a minimum value of 0 and a maximum value
of 2 32 -1. To learn more, visit How to use the unsigned integer in java
8?
* Default value: 0
5. long type
* The long data type can have values from -2 63 to 2 63 -1 (64-bit signed
Two's complement integer).
* If you are using Java 8 or later, you can use an unsigned 64-bit
integer with a minimum value of 0 and a maximum value of 2 64 -1.
* Default value: 0
Example 5: Java long data type
class LongExample {
public static void main(String[] args) {
Notice, the use of L at the end of -42332200000. This represents that it's
an integral literal of the long type. You will learn about integral literals
later in this article.
6. double type
* The double data type is a double-precision 64-bit floating-point.
* It should never be used for precise values such as currency.
* Default value: 0.0 (0.0d)
Example 6: Java double data type
class Main {
public static void main(String[] args) {
class Main {
public static void main(String[] args) {
Notice that, we have used -42.3f instead of -42.3in the above program.
It's because -42.3 is a double literal.
To tell the compiler to treat -42.3 as float rather than double, you need to
use f or F.
8. char type
* It's a 16-bit Unicode character.
* The minimum value of the char data type is '\u0000' (0) and the
maximum value of the is '\uffff.
* Default value: '\u0000'
class Main {
System.out.println(letter); // prints Q
String type
Java also provides support for character strings via java.lang.String
class.
Strings in Java are not primitive types. Instead, they are objects. For
example,
2. Local Variables
Local variables are declared in methods, constructors or
blocks. It must be initialized before first use.
3. Static Variables
Static variables are declared with the static keyword in a class, but
outside any method, constructor or block.
class A
3.3 Java Constant
As the name suggests, a constant is an entity in programming that is
immutable. In other words, the value that cannot be changed. In this
section, we will learn about Java constant and how to declare a
constant in Java.
What is constant?
Constant is a value that cannot be changed after assigning it. Java does
not directly support the constants. There is an alternative way to define
the constants in Java by using the non-access modifiers static and final.
Example of Enumeration
1. public class EnumExample
2. {
3. //defining the enum
4. public enum Color {Red, Green, Blue, Purple, Black, White, Pink, Gray
}
5. public static void main(String[] args)
6. {
7. //traversing the enum
8. for (Color c : Color.values())
9. System.out.println(c);
10. }
11. }
Output:
RED BLACK
GREEN WHITE
BLUE PINK
PURPLE GRAY
3.4 Java Class Attributes and Methods
Java Class Attributes
In the previous chapter, we used the term "variable" for x in the example
(as shown below). It is actually an attribute of the class. Or you could
say that class attributes are variables within a class:
Example
Create a class called "Main" with two attributes: x and y:
public class Main {
int x = 5;
int y = 3;
Another term for class attributes is fields.
Accessing Attributes
You can access attributes by creating an object of the class, and by using
the dot syntax (.):
The following example will create an object of the Main class, with the
name myObj. We use the x attribute on the object to print its value:
Example
Create an object called "myObj" and print the value of x:
public class Main {
int x = 5;
Create a Method
A method must be declared within a class. It is defined with the name of
the method, followed by parentheses (). Java provides some pre-defined
methods, such as System.out.println(), but you can also create your own
methods to perform certain actions:
A method must be declared within a class. It is defined with the name of
the method, followed by parentheses (). Java provides some pre-defined
methods, such as System.out.println(), but you can also create your own
methods to perform certain actions:
Example
Create a method inside Main:
public class Main {
static void myMethod() {
// code to be executed
Example Explained
* myMethod() is the name of the method
* means that the method belongs to the Main class and not an
object of the Main class. You will learn more about objects and
how to access methods through objects later in this tutorial.
* void means that this method does not have a return value. You
will learn more about return values later in this chapter
Call a Method
To call a method in Java, write the method's name followed by two
parentheses () and a semicolon;
In the following example, myMethod() is used to print a text (the action),
when it is called:
Example
Inside main, call the myMethod() method:
public class Main {
static void myMethod() {
System.out.println("I just got executed!");
public static void main(String[] args) {
myMethod();
Though both the objects are different, but since their values are same, so
the condition (s1.equals(s2)) is true.
3.8 Java Arrays
Here, the above array cannot store more than 100 names.
The number of values in a Java array is always fixed.
How to declare an array in Java?
In Java, here is how we can declare an array.
dataType[] arrayName;
* dataType - it can be primitive data types like int, char, double, byte,
etc. or Java objects
* arrayName - it is an identifier
For example,
double[] data;
// allocate memory
data = new double[10];
Here, the array can store 10 elements. We can also say that the size or
length of the array is 10.
In Java, we can declare and allocate memory of an array in one single
statement. For example,
Here, we have created an array named age and initialized it with the
values inside the curly brackets.
Note that we have not provided the size of the array. In this case,
the Java compiler automatically specifies the size by counting the
number of elements in the array (i.e. 5).
In the Java array, each memory location is associated with a number.
The number is known as an array index. We can also initialize arrays
in Java, using the index number. For example,
// declare an array
int[] age = new int[5];
// initialize array
age[0] = 12;
age[1] = 4;
age[2] = 5;
Java Arrays initialization
Note:
* Array indices always start from 0. That is, the first element of an
array is at index 0.
* If the size of an array is n, then the last element of the array will be at
index n-1.
How to Access Elements of an Array in Java?
We can access the element of an array using the index number. Here is
the syntax for accessing elements of an array,
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5, 2, 5};