Programming Basics Flashcards
How to output “ Hello World “ in CMD using Java?
public class Filename >1
{ >2
public static void main ( String[]args ) >3
{ >4
System.out.println(“Hello World”); >5
} >6
} >7
What does the world public mean in the first line of Java ?
- A keyword of Java language that indicates that the element that follows should be made available to other Java elements.
- Other class can also use it
What does the curly brackets ( {} ) do in 2nd line and the last line?
- Everything that are included in the curly brackets belongs to the class
What does the word void mean ?
- To indicate that no value is returned
- To let the program just run the program and return nothing
What does static mean ?
- To run the method without calling on the class
What does String [] args mean ?
- It is a parameter list
Why it was not advisable to create identifiers using the Java Keywords
- May lead to confusion
What is Java most concerned about ?
Case Sensitive
- for to For , it will be printing error
The most important programming tips
- Remember to semicolon ( ; )
- Remember to check if there is any word that are being written in Uppercase Letters
Java Keywords also known as
Reserved words
Statements format ( 5 )
1.Declaration Statement
- int i ;
- String name ;
- Assignment Statement
- i = 42 ;
- name = “ ADI “
- Declaration + Assignment
- int i = 42
- String name = “ ADI “
- Expression Statement
- System.out.println(“Hello World”)
- Many other more
- for
- if
Statement must type semicolon ( ; ) after finishing
- Declaration
- Expression
How can you create a block ?
- Using brace { }
Do we need to add semicolon after creating a block ?
No
Which is correct?
1. int Total
= 100;
2. int Total = 100;
Both are correct
What are double and float different ?
- double - to represent double-precision 64-bit floating-point numbers
- float - To represent single-precision 32-bit floating-point numbers
How to create a comment?
- Type // ( End-of-line Comments )
- Type /* */ ( Traditional Comments )
- ( Java DOC Comment )
All identifiers must begin with what?
Letter ( a , d , A)
What are the differences of state and behavior in Java?
- Consist data that the object might be keeping track of.
- Consists actions that the object can perform.
Key Principles of OOP
- Encapsulation
- Encapsulation is the bundling of data (attributes) and methods (functions) that operate on the data into a single unit, known as a class. - Abstraction
- Abstraction involves simplifying complex systems by modeling classes based on the essential properties and behaviors they share. - Inheritance
- Inheritance is a mechanism that allows a new class (subclass or derived class) to inherit the properties and behaviors of an existing class (superclass or base class). - Polymorphism
- Polymorphism allows objects of different classes to be treated as objects of a common superclass.
( PIEA )
What are usually used for variable names in Java ?
Camel case ( myClass1Object )
How to create another class
- Create a new class in a different file
- Then in the main file type :
Greater ( Another class file name ) myGreeterObject ( Hold the objects in the Greeter file ) = new Greeter(); ( Create new objects )
Variable must start with what first?
- Letter ( m,k,l ,small letter first [ camel case ] )
- Underscore ( _ )
- Dollar Sign ( $ )
What are class variable also known as?
Static Variables
What are the rules that the static variables follow?
- Must place within the class but not in the method ( Start of the code or the End )
- Must include the word static ( static int newNum = 5 )
What is a static variables ?
Variable that can be shared among the class
What is the usage of instance variables ?
To create us own class
All Variable
- Static Variable ( Outside of the method )
- Instance Variable ( Outside of the method )
- Local Variable ( Inside of the method )
- Final Variable ( Outside / Inside of the method )
What are the difference about Static and Local Variable?
- Static can put Above or Below the code
- Local must put Above the code
What is a Final Variable ?
The value can’t change after being initialized
How to add a Final Variable ?
Add a final word ( final int radius = 3.1415 )
Which variables a given default values ?
- Static Variable
- Instance Variable
All integer types
- byte ( -128 to 127 )
- short ( -32,768 to 32,767 )
- int ( -2,000,000,000 to 2,000,000,000 )
- long ( -9,000,000,000,000 to 9,000,000,000,000 )
Why there are so many integer types?
To save up some memory
How to make values easier to read ?
Putting underscore ( 58_473_882 )
All Floating-point types
- float ( F at the back )
- double ( D at the back )
All Character types
- char ( Uses ‘ ‘ )
- String ( Uses “ “)
What does var do in Java ?
- Decide the value is what type of data by itself
Why is string is written in Uppercase Letters ( String ) than other variable ( int , boolean )?
Because string isn’t primitive types, it was a reference types and it was the name provided by the Java API class
How to combine two strings?
Adding a + sign
How to convert an integer to string ?
Use the code Integer.toString() ( String s = Integer.toString (x); )
How to convert a string to primitives value ?
- int x = Integer.parseInt ( “ 100 “ )
- short x = Short.parseShort ( “ 100 “ )
- double x = Double.parseDouble ( “ 19.01 “ )
- boolean x = Boolean.parseBoolean ( “ true “ )
What are convert numeric data of one type to another called ? ( int to double )
- Casting
How to input data using JOptionPane?
String priceDialog;
priceDialog = JOptionPane.showInputDialog(“Please enter the price of the goods : “);
double goodsPrice = Double.parseDouble(priceDialog);
How to input data from Terminal?
static Scanner sc = new Scanner(System.in)
System.out.print(“Please enter the price of the goods : “);
double goodsPrice = sc.nextDouble();