Core Java Flashcards
Learning Objectives
To install the JDK using Synaptic
Package Manager.
To run java program, we need to install JDK and Java development kit
Why Java?
Simple Object oriented Platform independent Safe High performance Multi-threaded
Types and applications of Java.
Types and Applications of Java JSP or Java Server Pages Java Applets J2EE or Java Enterprise Edition JavaBeans Mobile Java
Create a simple java program.
class HelloJava { public static void main(String[] args){ System.out.println("My first Java program"); } }
Description :- Public - access modifier, visible to all Static - no need to create object to invoke static method Void - return type. Main - starting point System.out.println - print statement
Naming Conventions in Java
The class name should be in CamelName Example:- PayPal, iPhone, MasterCard.
The method name should be in mixed case
Also method name should be an verb
Example :- showString(), main(),
goToHelp().
Variable name should not begin with digits
We cannot use keywords for class, method and variable name
Example:- Cannot use keywords like
public, private, void, static and many
more.
Install Eclipse on Ubuntu
Install Eclipse on Redhat
fetch software update
-> sudo apt-get update
install software -> sudo apt-get install eclipse Ubuntu - Similar OS Debian Kubuntu & Xubuntu
fetch software list
->sudo yum upate
install software
->sudo yum install eclipse
Redhat - Similar OS
Fedora
Centos
SUSE Linux
—-Introduction to Eclipse—-
What is Eclipse?
Eclipse is a Integrated development environment. One can write, debug and run Ava program with ease
Possible Errors
Missing semicolon Missing double quotes Mis-match file name and class name Typing print statement in lower case
Programming Features Used in
Eclipse IDE
Auto completion
Syntax error highlighting
Error dialog box
Shortcut keys
Data types in Java
There are in total 8 data types defined in Java programming known as the primitive type
Numerical data type
int, float
Arithmetic Operations
Addition : + Subtraction : - Multiplication : * Division : / Modulo Operation(Remainder after division) : %
String Create strings Add strings Convert to lower case Convert to upper case
String in Java, is a sequence of
characters
public class StringDemo { public static void main(String agrs[]){
String greet = "HeLlO"; String name = "JaVa"; char SPACE = ' '; greet = greet.toLowerCase(); name = name.toUpperCase(); String msg = greet + SPACE + name; System.out.println(msg); } }
Output :-
hello JAVA
Type Conversion
Converting one data type to another
Type Conversion
Converting one data type to another
Java facilitates type conversion in two forms:
Implicit (Automatic) Type Conversion
Explicit Type Conversion
Implicit type conversion
Also called as widening conversion because the compiler converts the value of (smaller size) type to the boarder(large size)
byte ——— is convertible to ————-> short, int, long, float, or double
Short ——— is convertible to ————-> int, long, float, or double
char ——— is convertible to ————-> int, long, float, or double
int ——— is convertible to ————-> long, float, or double
long ——— is convertible to ————-> float or double
float ——— is convertible to ————-> double
Explicit type conversion
It is also called as narrowing conversion(narrowing of broader data type to smaller data type). The type casting is done manually by the programmer and not by the compiler
Integer toString(int i) Float toString() Method
Java toString() method is a part of the Integer class of the java.lang package used to convert integer value into String.
Syntax:-
Integer.toString(a)
Java toString() method is a part of the float class if the java.lang package used to convert float value to string
Syntax
a.toString()
Relational operators
Used to check the condition
The size pf boolean data type is 1 bit
It stores only two values(true/false)
Logical operators
They are used to check multiple conditions
and &&
or ||
not !
Conditional Statements
Control the flow of execution of the program
Types:
If : single conditional statement. Executes a block of statements
If-else : Single condition. Executes alternatives statements
if else if : Multiple conditions. Executes various sets of statements. Called as branching or decision making statement
nested if : An if statement inside another if statement
switch
Rules for conditional statement, ternary operator
Add a semicolon for terminating the sentence
Do not add colon after the condition
Add block of code within curly braces
ternary operator : conditional operator providing similar results as if else statement. Short syntax and denoted by ?. Takes three operands at a time.
Syntax :-
Result = Expression ? operand 1 : operand 2
Arrays
Marks: 15, 12, 13, 11, 12, 10
Names: Arun, Bala, David
Temperatures: 33.2, 31.7. 32.3
Rainfall: 25, 31, 29, 13, 27, 3
An array is a collection of similar data types
Array Operations
Arrays.toString
copyOfRange() method
import java.utiil.Arrays;
Arrays.toString
Converts any datatype of of array to string
–> Arrays.toString(var_name)
copyOfRange() method
This method ensures that the continuity of of ranges is maintained. It receives a copy of array from starting index and the ending index of the array
(0,4) implies from index 0 to 3
int[] array = {1,2,3,4,5,6,7};
int[] array1 = Arrays.copyOfRange(array , 3,5);
for(int num : array1)
System.out.println(num);
–> 4,5
Array sort() Array fill() Array copyOf() Arrays.equals()
Array sort() method Sort method of array class. It accepts array and sorts them in ascending order. We can extend this feature by providing start_index and end_index
Arrays.sort(arr);
Array fill() method fill() method of array class. We can assign a particular value to all array elements or in a specified range
Arrays.fill(arr, 5);
Array copyOf() Similar to copyOfRange() method
Arrays.copyOf(arr, 4);
Arrays.equals() array
Compare two array by comparing each elements of the array.