Java OOPC - Object Generation, Data Types, Parameters and Arguments
Object Generation
Object
generation in java is the process of creating objects of a class. Using class
reference and using class constructor object is generated .
They
are created using the new keyword followed by a constructor.
We can create any number of object for specific class and there is no limit for it. Object add more reliable code.
public
class ObjectGenerationExample {
public static void main(String[] args) {
// Creating objects
Car car1 = new Car();
Car car2 = new Car();
// Using objects
car1.displayInfo();
car2.displayInfo();
}
}
Data Types
Java
is a statically-typed programming language, which means that the data types of
variables must be explicitly declared before using it. Understanding the
various data types available in the Java help us to writing efficient and more
correct errorless code.
Further,
there are two main data types.
1.Primitive
Data type
2.Reference
Data type
1.Primitive
Data type
Firstly,
if we get primitive data type mainly there are eight data types. Those are
Boolean, char, byte, short, int, long, float and double.
1. boolean type:
A boolean data type can
only store True or False values.
2. byte type:
The smallest data type between
all integer data types is the byte.
3. short type:
The short data type
(16-bit signed two’s complement integer) can store whole numbers from -32768 to
32767.
4. int type:
The int data type (32-bit signed two’s complement integer) can store whole numbers from -2147483648 (-2^31) to 2147483647 (2^31 -1).
5. long type:
The longest data type
among all integer data types is the long (64-bit two’s complement integer).
6. char type:
The char data type is a
single 16-bit Unicode character and is used to store a single character. It
stores both lower case and upper case characters which must be enclosed in
single quotes.
7. float type:
It is a form of
floating-point data that stores values with decimal precision.
8. double type:
The double data type
resembles the float data type.
2.Reference
Data type
Non-primitive
data types are called reference types because they refer to objects.
Arrays, class ,interface, string, Enum
are the reference data types, and if we consider about the interface it similar
to class. String ,Enum are classes and String we used as a data type.
Java Parameters and arguments.
Parameters in Java
Parameters act as placeholders
for valus that will be provide when the
method is called. Parameters define the input that a method expects to receive.
Here’s an example of a method with parameters:
public class ParameterExample {
// Method with two
parameters
public static void
printSum(int a, int b) {
int sum = a + b;
System.out.println("Sum: " + sum);
}
public static void
main(String[] args) {
// Calling the
method with arguments
printSum(5, 7);
}
}
Arguments in java
Arguments are the actual
values passed to a method when it is called. They match the data types and order
of the parameters defined in the method. When a method is called, the values provided
as arguments are assigned to the corresponding parameters within the method
body.
According to the above
example, printSum(5,7) ,the values 5,7 are arguments. They are passed to the
parameters a and b when the printSum method is invoked.
printSum(5, 7); // 5 and
7 are arguments
Some facts about the
parameters and arguments:
- A method must include a
return type.
- When, Method type is
void(Default), from that we can return any data type, but when data type become a return type that data type only
returned. Also It takes that data types only.
Comments
Post a Comment