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

Praktisi Mengajar 2022: Java Primitive & Non-Primitive Data Types

Mata Kuliah: Dasar-Dasar Pemrograman Program Studi: S1 Sistem Informasi Fakultas Teknik Perguruan Tinggi: Universitas Persada Indonesia Y.A.I Dosen: Drs. Ahmad Rosadi, M.Kom Praktisi: Lintang Wisesa Atissalam, S.Si

Uploaded by

Lintang Wisesa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views

Praktisi Mengajar 2022: Java Primitive & Non-Primitive Data Types

Mata Kuliah: Dasar-Dasar Pemrograman Program Studi: S1 Sistem Informasi Fakultas Teknik Perguruan Tinggi: Universitas Persada Indonesia Y.A.I Dosen: Drs. Ahmad Rosadi, M.Kom Praktisi: Lintang Wisesa Atissalam, S.Si

Uploaded by

Lintang Wisesa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

<Dasar-Dasar Pemrograman Java/>

PRIMITIVE & NON-PRIMITIVE


DATA TYPES IN JAVA
lintangwisesa
<Dasar-Dasar Pemrograman/>

BAHASA PEMROGRAMAN JAVA


lintangwisesa
<HELLO/>

🧑 Hi, I’m lintangwisesa!


💼 System Developer @ Smartfren
🎓 S1 Fisika UGM & S2 MTI Fasilkom UI
📧 lintangwisesa @ ymail.com
📞 +62.858.900.900.45
Lintang Wisesa
<Dasar-Dasar Pemrograman - Eps.01/>

Dasar-dasar Pemrograman Prosedural:


1. Pengertian Method dan Atribute
2. Pemrograman dengan Static Method
3. Penggunaan Flow of Control
4. Pemanggilan Method
<JAVA/>
Java is a general-purpose programming language released in 1995
by Sun Microsystems, intended to let programmers write once,
run anywhere (WORA). Java applications are typically compiled
to bytecode that can run on any Java virtual machine (JVM)
regardless of the underlying computer architecture. The syntax
of Java is similar to C and C++, but has fewer low-level
facilities than either of them. Download & install: java.com.
Usage:
● Backend development
● Android app development
● Desktop application
● Web server
● Big data
<TOP 10 LANG/>

Source: https://pypl.github.io/PYPL.html (Sept 2022)


<TOP 10 IDE/>

Source: https://pypl.github.io/IDE.html (Sept 2022)


<LET’S CODE/> Review last meeting

public class Main{


public static void main(String[] args){
System.out.print("Hello World");
}
}

Go to VScode =>
<VARIABLES/>

2x + 2 = 12
Berapa nilai x?

x + y = 5
x - y = 1
Berapa nilai x dan y?
<VARIABLES/>

Aku adalah mamalia berkaki empat.


Kulitku ada yang berwarna putih, hitam,
cokelat atau kombinasi ketiganya.
Manusia biasa mengkonsumsi daging dan
susuku. Siapakah aku?
<VARIABLES/>
Variables are containers for storing data values. To create a
variable in Java, you must specify its type and assign a value:

dataType identifier = value;


Identifiers are unique names which identified variables. It can be
short names (like x or y) or more descriptive names (like name,
age, isMarried or totalPrice ).

Data type specifies the size and/or type of variable values. In


Java there are 2 groups of data types: primitive data types (byte,
short, int, long, float, double, boolean and char) and
non-primitive data types (String, Array and Class).
<IDENTIFIERS/>
Identifiers are unique names which identified variables. It can be
short names (like x or y) or more descriptive names (like name,
age, isMarried or totalPrice ). The general rules for naming
variables are:

● Must begin with a letter.


● Can contain letters, digits, underscores and dollar signs.
● Should start with a lowercase and it can’t contain whitespace.
● Can also begin with $ and _.
● It’s case sensitive (myAge and myage are different).
● Reserved words (like int or boolean) can’t be used as names.
<DATA TYPES/>
Data type specifies the size or type of variable
values. In Java there are 2 groups of data types:
● Primitive data types, like byte, short, int,
long, float, double, boolean and char. A
primitive data type specifies the size and type
of variable values, and it has no additional
methods.
● Non-primitive data types, for instance String,
Array and Class.
<PRIMITIVE DATA TYPES/>
DATA TYPE SIZE DESCRIPTION

boolean 1 bit Stores true (1) or false (0) values

byte 1 byte Stores whole numbers from -128 to 127

short 2 bytes Stores whole numbers from -32,768 to 32,767

int 4 bytes Stores whole numbers from -2,147,483,648 to 2,147,483,647

long 8 bytes no -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807

float 4 bytes Stores fractional numbers, 6 to 7 decimal digits

double 8 bytes Stores fractional numbers, 15 decimal digits

char 2 bytes Stores a single character/letter or ASCII values


<NON-PRIMITIVE DATA TYPES/>
Non-primitive data types, like String, Arrays, Class, Interface, etc are
called reference types because they refer to objects. The main difference
between primitive and non-primitive data types are:
● Primitive types are predefined (already defined) in Java.
Non-primitive types are created by the programmer and is not defined
by Java (except for String).
● Non-primitive types can be used to call methods to perform certain
operations, while primitive types cannot.
● A primitive type has always a value, while non-primitive types can
be null.
● A primitive type starts with a lowercase letter, while non-primitive
types starts with an uppercase letter.
● The size of a primitive type depends on the data type, while
non-primitive types have all the same size.
<LET’S CODE/> Variables, identifier & data types

public class Main{


public static void main(String[] args){
String nama = "Budi";
}
}

Go to VScode =>
<STRINGS/>
Strings are used for storing text. A String variable contains a
collection of characters surrounded by double quotes:

String halo = "Hello World!";


A String in Java is actually an object, which contain methods that can
perform certain operations on strings.

teks.length()
teks.toUpperCase()
teks.toLowerCase()
teks.indexOf( "e")
<ARRAYS/>

BALLS FRUITS
<ARRAYS/>

Arrays are used to store multiple values in a single


variable, instead of declaring separate variables for
each value. To declare an array, define the variable
type with square brackets.

String[] balls = {"⚽","🏀","⚾","🏈","🏐"};


String[] fruits = {"🍍","🍉","🍇","🍊","🍎"};
<OPERATORS/>

Operators are used to perform operations on


variables and values. Java divides the operators
into the following groups:
● Arithmetic operators
● Assignment operators
● Comparison operators
● Logical operators
<LET’S CODE/> Operators

public class Main{


public static void main(String[] args){
System.out.println(5+2-1/3);
}
}

Go to VScode =>
<METHODS/>
<METHODS/>

A method is a block of code which only runs when it is called.


Methods are used to perform certain actions, and they are also
known as functions. You can pass data, known as parameters,
into a method. Methods can be used to reuse code: define the
code once, and use it many times.

static void myMethod() {


System.out.println("Hello World!");
}
<PARAMETERS & ARGUMENTS/>

Information can be passed to methods as parameter. Parameters


act as variables inside the method. Parameters are specified
after the method name, inside the parentheses. You can add as
many parameters as you want, just separate them with a comma.

static void Kalikan(int x, int y) {


System.out.println(x * y);
}
<PARAMETERS & ARGUMENTS/>

When a parameter is passed to the method, it is called an


argument. So, from the example above: x & y are parameters,
while (4,20), (6,7) and (10,9) are arguments.

Kalikan(4, 20);
Kalikan(6, 7);
Kalikan(10, 9);
<RETURN METHODS/>
The void keyword, used in the examples before, indicates that
the method should not return a value. If you want the method
to return a value, you can use a primitive data type (such as
int, char, etc.) instead of void, and use the return keyword
inside the method.

static int myMethod() {


return 5;
}
<LET’S CODE/> Methods, Params & Args

public class Main{


public static void main(String[] args){
myMethod(1, 12);
}
}

Go to VScode =>
<CONTROL FLOW STATEMENTS/>

Generally, the code executes from top to bottom. But


sometimes you have to make some decisions that will decide
which block of code executes first and which one later.
Control flow statements let you control the flow of the
execution of the code in your program. In Java programming
language, you can control the flow of execution of the
code by placing the decision making, branching, looping,
and adding conditional blocks.
<CONTROL FLOW STATEMENTS/>

DECISION MAKING LOOPING BRANCHING

Use when we have to Executing the block jump the flow of


decide which block of code multiple execution from one
of the code will be times until the part of a program to
executed. condition is true. another.

* if statement * for loop * break statement


* If-else statement * while loop * continue statement
* switch statement * do-while loop * return statement
<IF STATEMENT/>

Use the if statement to specify a block of Java code to be


executed if a condition is true.

if (condition) {
// block of code to be executed
// if the condition is true
}
<IF-ELSE STATEMENT/>
Use the else statement to specify a block of code to be
executed if the condition is false.

if (condition) {
// run if the condition is true
} else {
// run if the condition is
false
}
<SWITCH STATEMENT/>
switch(expression) {
The switch expression is evaluated
case x:
once. The value of the expression is
// code block
compared with the values of each
break;
case y: case. If there is a match, the
// code block associated block of code is executed.
break; break keyword will breaks out of the
default: switch block. The default keyword
// code block specifies some code to run if there
} is no case match
<END/>

public class Main{


public static void main(String[] args){
String thx = "Terima kasih 🙏";
}
}

See you =>


<Dasar-Dasar Pemrograman - Eps.02/>

Definite Loops dan Indefinite Loops:


1. Pendefinisian Loop
2. Penggunaan Definite Loop dengan FOR
3. Penggunaan Loop bersarang (Nested Loop)

You might also like