0% found this document useful (0 votes)
9 views10 pages

Java Identifiers and Method Basics

The document discusses Java programming concepts, including the use of identifiers, method return values, and the significance of the main method. It outlines the rules for naming identifiers, the structure of the main method, and the differences between primitive and non-primitive data types. Additionally, it provides examples and explanations of various data types and their characteristics.

Uploaded by

Mohit Gangwani
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)
9 views10 pages

Java Identifiers and Method Basics

The document discusses Java programming concepts, including the use of identifiers, method return values, and the significance of the main method. It outlines the rules for naming identifiers, the structure of the main method, and the differences between primitive and non-primitive data types. Additionally, it provides examples and explanations of various data types and their characteristics.

Uploaded by

Mohit Gangwani
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

​6)​​We cannot use all the 50 java language keywords as identifier​

​names.​
​7)​​We can take any length as an identifier length.​
​8)​​We can take java class names as identifier names also but it is​
​highly not recommended.​

​ eparators.​
S
​() -> Method parameters and conditions​

​{} -> Represents a block of a code​

​[ ] -> It is a Array dimension/representation​

​; -> Terminates the statement​

​, -> Separates multiple parameters/variables​

​. -> To call a method, to specify the package name​


​—-----------------------Day 19 29th july—----------------------------​

​Returning a value from a java method.​

​ ) When will the Java compiler come back to the calling method ?​
Q
​-> Java Compiler will be coming back to the calling method in the​
​below scenarios.​

​ After executing all the statements present inside a method​


1
​2 Whenever the compiler comes across the return statement​
​immediately it will come back to the calling method.​
​3 If there is an exception.​

​Keypoints​
​ A return statement​​need not be the last statement​​inside a​
1
​method, but it​​should be the last executable statement​​inside a​
​method.​

​ Inside a void method we can write a return statement without​


2
​returning anything.​

​ It is not mandatory inside void methods, except void method any​


3
​other method return types 100% we need to write the return​
​statement.​

​4 Return type and return value should be compatible and not same.​

​We have three control sections​


​1)​​Selection​
​2)​​Iteration​
​3)​​Jump​

​Character data type should be kept inside single quotes​

​ ---------------------Day 20 Video not​



​available—--------------------------------​
​Understanding the main method why it is declared as public static void​
​main(String[] args)​

​public static void main(String[] args)​

​Word​ ​Meaning / Significance​


​public​ I​t is an​​access modifier​​. It means the method is​
​accessible from​​anywhere​​. The JVM must be able to​
​access this method to start the program.​

​static​ ​ eans this method belongs to the​​class​​, not to an​​object.​


M
​JVM can call it​​without creating an object​​.​

​void​ ​ eturn type​​. It means the method​​does not return any​


R
​value​​.​

​main​ ​ ethod name​​. It is the​​entry point​​of every Java​


M
​program. Execution begins from here.​

​(​,​​)​ ​ arentheses used to define the​​parameter list​​for​​the​


P
​method.​

​ tring[​ P
S ​ arameter​​to the main method. It is an​​array of strings​
​] args​ ​that stores​​command-line arguments​​passed while​
​running the program. For example:​​ java Test hello​
123​​will store:​​
​ args[0] = "hello"​​and​​
args[1] =​
"123"​

​[]​ args​​is an​​array​​.​


​Shows that​​

​args​ ​ ariable name (can be any name). Used to access​


V
​command-line arguments.​

​Full Meaning in one sentence​

​ he line defines a​​public method named​​


T main​​, which​​returns​
​nothing​​, is​​static​​so JVM can call it directly, and​​accepts​​an array of​
​strings​​as input arguments.​

​—-------------------------Day 21 1st august—----------------​


​ ​
N ​Syntax​ ​ orrect /​
C ​Explanation​
​o.​ ​Incorrect​

​1​ public static​ ​✔️ Correct​ S


​ ​ tandard JVM-recognized​
void​
​ ​signature.​
main(String[]​

args)​

​2​ public static​ ​✔️ Correct​ S


​ ​ pace does not matter; same​
void​
​ ​as above.​
main(String []​

args)​

​3​ public static​ ​✔️ Correct​ A


​ ​ rray brackets can be placed​
void​
​ ​next to type or variable.​
main(String​

[]args)​

​4​ public static​ ​✔️ Correct​ A


​ ​ rray notation after variable​
void​
​ ​name is allowed.​
main(String​

args[])​

​5​ public static​ ❌


​ ​ ​ ​]​​cannot precede the type;​
[
void​
​ ​Incorrect​ ​must come after type or​
main([]String​
​ ​variable name.​
args)​

​6​ public static​ ​✔️ Correct​ K


​ ​ishan​​is only a variable​
void​
​ ​name; can be anything.​
main(String[]​

Kishan)​

​7​ static public​ ​✔️ Correct​ O


​ ​ rder of​​public​​and​​ static​
void​
​ ​does not matter. JVM only​
main(String[]​
​ ​needs correct return type​
args)​
​ ​before method name.​

​8​ public static​ ❌


​ ​ ​ ​ VM requires​​
J void​​. Different​
int​
​ ​Incorrect​ ​return type means JVM will​​not​
​recognize​​it as entry point.​
main(String[]​

args)​

​9​ public static​ ​✔️ Correct​ f


​ ​inal​​is allowed. It just​
final void​
​ ​prevents overriding (static​
main(String[]​
​ ​already prevents overriding).​
args)​

Public static​ ❌
​10​ ​ ​ ​ ​ ava is case-sensitive. Must be​
J
void​
​ ​Incorrect​ ​lowercase​​public​ ​.​
main(String[]​

args)​

​11​ ​
final public​ ​✔️ Correct​ ​Modifiers order is flexible.​
static void​

main(String[]​

args)​

​12​ ​
Final public​ ❌
​ ​ final​
​Must be lowercase​​ ​.​
static void​
​ ​Incorrect​
main(String[]​

args)​

public static​ ​✔️ Correct​ U


​13​ ​ ​ ses varargs (​​
...​ ​) instead of​
void​
​ ​array. Works same as​
main(String...​
​ String[] args​
​ ​.​
args)​

public static​ ❌
​14​ ​ ​ ​ main​
​ VM looks for​​
J mian​
​, not​​ ​.​
void​
​ ​Incorrect​ ​Case-sensitive.​
mian(String[]​

args)​

public static​ ❌
​15​ ​ ​ ​ ​ rray size cannot be specified​
A
void​
​ ​Incorrect​ ​in method parameter​
main(String[8]​
​ ​declarations.​

args)​

public static​ ❌
​16​ ​ ​ ​ ​ ompiles, but JVM does not​
C
void​
​ ​Incorrect​ int[]​​in main entry​
​accept​​
main(int[]​
​ ​for JVM​ String[]​
​signature; must be​​ ​.​
​entry​
args)​

public static​ ❌
​17​ ​ ​ ​ String[]​​or​
​Must accept a​​
void main()​
​ ​Incorrect​ String...​​argument.​

​18​ ​
public void​ ❌
​ ​ ​ ust be​​
M static​ ​, otherwise​
main(String[]​ ​Incorrect​
​ ​JVM cannot call it without​
args)​
​ ​object creation.​

public static​ ❌
​19​ ​ ​ ​ ​ain​​≠​​
M main​
​. Case-sensitive​
void​
​ ​Incorrect​ ​method name required.​
Main(String[]​

args)​

​ ata types​
D
​There are two types of data types​
​1)​​Primitive data types​
​2)​​Non primitive/userdefined/referenced data types​

​1. Primitive Data Types​


​Primitive types are​​basic built-in data types​​.​
​They​​store simple values​​, not objects, and do not​​require extra​
​memory.​

​Type​ ​Description​ ​Example default values​

​byte​ ​ -byte integer​


1 byte b = 10;
​ 0​
​value​

​short​ 2
​ -byte integer​ short s = 200;
​ 0​
​value​

​int​ ​ -byte integer​


4 int a = 1000;
​ 0​
​value​

​long​ ​ -byte integer​


8 long l = 100000L;
​ 0L​
​value​

​float​ ​ -byte decimal​ ​


4 float f = 5.6f; 0.0f​
​value​

​ -byte decimal​ ​
​ oub​ 8
d double d = 5.899; 0.0d​
​le​ ​value​

​ oole​ ​true / false​


b boolean flag = true;
​ false​
​an​

​char​ ​ ingle​
s char c = 'A';​
​ null/space/?​

​character​ Default value​​


​ /u0000​

​📌 Characteristics of Primitive Types​

​●​ ​Not objects (stored in​​stack memory​​)​

​●​ ​Fast and memory efficient​


​●​ ​Cannot have methods​

​2. Non-Primitive (Reference / Object) Data Types​

​Non-primitive types​​refer to objects​​.​


​They store​​memory addresses (references)​​rather than​​actual​
​values.​

​Type​ ​Description​ ​Example Default values​

​String​ ​Sequence of characters​ ​


String s = "Hello"; null​

​Array​ C
​ ollection of similar​ int[] arr = {1,2,3};
​ null​
​elements​

​Class​ U
​ ser-defined blueprint​ class Student {...}
​ null​
​for objects​

​Object​ ​Instance of a class​ Student st = new Student();


​ null​

I​nterfa​ ​Contract of methods​ interface Shape {...}


​ null​
​ce​

​Enum​ ​Constant values​ enum Level {HIGH, LOW}


​ null​

​📌 Characteristics of Non-Primitive Types​

​●​ ​Created by the user (except String)​

​●​ ​Stored in​​heap memory​

​●​ ​Can have methods​

You might also like