—— JAVA ——
1. WRITE A PROGRAM JAVA TO ADD ANY TWO NUMBERS.
Ans:
declare class {
declare main method {
declare variable with initializing the values;
add these two numbers then store in another variable
display the result
}
}
—Keywords in Java —
public, void
comment in Java starts with “ // “
void is return type of main method
String — Class first line in code
Array - more values in single variable, (To use in Code)
Case Sensitivity = Yes ✅
Class Name First letter of class name should be Upper Case
Eg: public class Myfirstcode {}
OBJECTIVES
Comments -
Identifiers -
Primitive Datatypes Examples
Keywords -
Syntax and semantics
Indentation -
Variable - Done to hold the value in computer || local variable, instance and
static variable ||
Datatype - Types of the data || Float, double, integer, boolean(1 or 0) - || 1 bit
||, byte(), short (-128 to 127) #shortdatatypesrange , char(28317893271931), ||
Long can hold all of the data type but the computer will occupy 64 byte of space so
short or int is used for shorter variable instead of long || Memory Efficiency
Increases ||
Java Literals -
Escape sequence -
Operator - +, -, *, %, / Assignment operator is “=“ where = operator changes the
value ||
Logical operator and, or , not in and operator
Conditional operator (&&)
if 10/4 is given then in java the output is also in int format.
if 10/4.0 then the output is in float form.
Increment and decrement: a++ or b++ or a-- or b--
a=5;
print(a++)- postfix increment first a prints itself and only then increment is
done.
print(++a)- prefix increment, it will increase the variable before printing itself.
Constants - final String DEFAULT_MODULE = “Programming”;
String value of default module is programming. final makes variable value constant.
For printing the output with “ “
use \" for printing the output with “ “ :
Example:
printLn(\"Argument\")
New line: \n
Escape Sequence:
\t insert a tab
Example:
a = 10, b = 10
(a++ + ++a) = 22
(b++ + b++) = 21
Ternary Operator:
Example:
a=2
b=5
int min = (a<b)?a:b;
System.out.println(min); // Prints lower value
Write a program to display the series of 2,3,5 up to 10th term using for loop,
while loop and do loop.
WAP to display the square of first 10 natural numbers.
WAP to find the square of first 10th numbers.
————————————————————
Modifiers: Public
Return Type: result type | CheckBalance(){}
Method Name:
Method type should be same as value, RETURN type to be considered.
public int add (int a, int b )
Modifier, return type, methodName, ParameterName:
public int add(int a, int b)
METHOD CALLING,
METHOD DEFINE:
Modifier return type method name (parameter)
SumDigit obj = new SumDigit ();
obj.digitsum(); // public void digitsum()
int result = obj.digitsum(x);
System.out.println(result);
obj = where the value is stored
new = for creating new variable
SumDigit(); = same as class name
SumDigit = type
Static Method can be called by four ways:
- With class name
- With reference variable
- With dynamic object
- With reference variable which contains null
Formal Argument = Argument specified with the method definition or declaration.
Actual Argument = Arguments specified with method calling.
Method Overloading:
Write multiple methods by changing the parameters with same name.
Rules:
- Method name should be same
- Parameters should be different (types, number, order)
Constructors:
- Special type of method which is invoked automatically at the time of object
creation.
- Should be same as the class name.
- Creating object will automatically run constructor.
- Constructors are used to create objects for a class.
- It looks like method declaration.
- Two types: Constructor Overloading (same as method overloading).
This keyword = reference variable.
Local variable “Can be used only in body” = local variable.
If not static, you need to create an object to use the variable.
If inside some body the given:
variable = 10
and in class variable it's 5,
when printing inside the body with, this.variable it will return the variable which
is outside the given body but if just done variable then it will return the value
which is inside the body.