0% found this document useful (0 votes)
4 views36 pages

Java Programming Basics Explained

The document provides an overview of programming concepts, focusing on Java, including definitions of programming, its importance, paradigms, and specific features of Java such as object-oriented principles, data types, variables, methods, and exception handling. It also covers tools for programming, types of operators, conditional statements, loops, and the principles of object-oriented programming (OOP) like inheritance, encapsulation, polymorphism, and abstraction. Additionally, it includes sample code snippets for various programming constructs and explanations of access modifiers and interfaces.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views36 pages

Java Programming Basics Explained

The document provides an overview of programming concepts, focusing on Java, including definitions of programming, its importance, paradigms, and specific features of Java such as object-oriented principles, data types, variables, methods, and exception handling. It also covers tools for programming, types of operators, conditional statements, loops, and the principles of object-oriented programming (OOP) like inheritance, encapsulation, polymorphism, and abstraction. Additionally, it includes sample code snippets for various programming constructs and explanations of access modifiers and interfaces.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link] is programming.

a)Programming is way to instruct the computer to perform a


task in a specified manner..
2….why programming is important?
A))now a days so much of our world is [Link] gives
interaction between the human and the machine.
3….programming paradigms?
Programming paradigms are two types
[Link]
[Link]
1)imperative divided into sub types major types are
[Link] programming
[Link]
2)declarative divided into sub types they are:
[Link] programming
[Link] programming
4….why we use java?
a)Java is a object oriented programming [Link] is used
to develop any kind of applications like desktop
Application and web applications etc.
5….About java?
a))[Link] was introduced by james gosling in 1991 but it was
first implemented in 1996.
[Link] was introduced by sun micro system.
[Link] currently maintained by oracle corporation.
Advantages:
[Link]
[Link] independent
[Link]
[Link]
Etc..
Disadvantages:
[Link] performance than c++.
[Link] good ui.
6….what are tools available for write programming.
a)[Link]
[Link] idea
[Link]
[Link] studio code.

Program:
Public class Test{
Public static void main(String[] args){
[Link](“hello world”);
}
}
7..what is PaCkage?
A)pakage is a container. which is used to segregates the files
in java project.
8..What is class.?
a)Class is a container. which is used to store entire
information of class.
Members of class are:
[Link]
[Link]
Program:
Public class Dog{
Public string breed=”german shephard”;
Public int weight=”20kg”;
Public int height=”2 feet”;
Public static void main(String[] args){
Dog dog=new Dog();
[Link](“[Link]”);
[Link](“[Link]”);
[Link](“[Link]”);
}
}
9..what are the data types?
Types are:
[Link]
[Link] primitive
1)primitive divided into three types:
[Link]
[Link]
[Link]
Numbers divided into two types:
[Link]:
[Link]
[Link]
[Link]
[Link]
[Link]:
[Link]
[Link]
2)Boolean:
*True or false
Program:
Public class Datatypes{
Public static void main(String[] args){
[Link](“[Link]”\8);
[Link](“Byte.MIN_VALUE);
[Link](“Byte.MAX_VALUE);
}
}
Non primitive data types are:
[Link]
[Link]
[Link]
[Link]
10….what is variable?
A)variable is a container it is used to store values.
Types are:
[Link] variables
[Link] variables
[Link] variables
[Link].
[Link] for instance variables:
Public class Variables{
Int id=”205”;
Public Static void main(String[] args){
Variables va=new Variables();
[Link](“[Link]”);
}
}
[Link] for class variable:
Public class Variables{
Static Int id=”205”;
Public Static void main(String[] args){
[Link](“[Link]”);
}
}
[Link] for local variables:
Public class Variables{
Public Static void main(String[] args){
Int id=”205”;
Variables va=new Variables();
[Link](“[Link]”);
}
}
[Link] for parameters:
Public class Variables{
Public Static void main(String[] args){
Test1(70);
Public Static void test1(int height){
Int height=”200”;
[Link](“height”);
}
}
11..what is a method?
a)method is a block of code or group of statements
work together to perform a task and it return caller if
capable.
Types of methods:
[Link] method
[Link] static method
Program:
Public class Rat{
Public static void weight(){
[Link](“half kg”);
}
Public void food(){
[Link](“grains”);
}
Public static void colour(){
[Link](“black”);
}
Public void main place(){
[Link](“holes”);
}
Public static void main(String[] args){
[Link]();
Rat f=new Rat();
[Link]();
[Link]();
Rat p=new Rat();
[Link]();
}
}
11..Different types of print satments?
a)[Link]:it is used in formatting strings
[Link]:cursor will be place in the same line.
[Link]:cursor move to the next line.
12..what is operator?
a)operator is a symbol used to perform operations on
variables or values.
Types of operators:
[Link] operator:
[Link] operator
[Link] operator
[Link] operators
[Link] operators
[Link] operators:--
 Addition
 Subtraction
 Multiplication
 Divison
Modulus
2..unary operators:
 Increment
 Decrement
3..Relational operators:
 >=
 <=
 ==
 !=
 <
[Link] operators:
 And(&&)
 Or(||)
[Link] operators:
 +=
 -=
 /=
 %=
 *=
 =

1..Program for Arthmetic operators:


Public class Operators{
Public static void main(String[] args){
Int i=20;
Int j=10;
[Link](i+j);
[Link](i-j);
[Link](i*j);
[Link](i/j);
[Link](i%j);
}
}
2..program for unary operators.
Public class Operators{
Public static void main(String[] args){
Int i=20;
[Link](i) ;
[Link](i++);
[Link](i--);
[Link](i);
[Link](++i);
[Link](--i);
}
}

3..program for Relational operators:


Public class Operators{
Public static void main(String[] args){
Int i=20;
Int j=10;
[Link](i==j);
[Link](i!=j);
[Link](i<j);
[Link](j>i);
[Link](i<=j);
[Link](i>=j);
}
}

[Link] for conditional operators:-


Public class Operators{
Public static void main(String[] args){
Int i=10;
Int j=20;
[Link](i<j&&i<=j);
}
}
[Link] for Assignment operators:
Public class Operators{
Public static void main(String[] args){
Int i=10;
I+=5;
[Link](i);
}
}
[Link] of conditional statements?
a)[Link]
[Link]-else
[Link]
Program for if statement:
Public class decisionmaking{
Public static void main(String[] args){
Scanner scanner=new Scanner([Link]);
[Link](“enter a number”);
int number=[Link]();
if(number<0){
[Link]("this is negative number ");
}

}
}

2..program for if else statement:


public class Decisionmaking{
public static void main(String[] args){
Scanner scanner=new Scanner([Link]);
[Link]("enter first number");
int number=[Link]();
if(number%2==0) {
[Link]("even number");
}
else {
[Link]("odd number");
}

}
}
[Link] for switch statement:
import [Link];

public class Decisionmaking{


public static void main(String[] args){
Scanner scanner=new Scanner([Link]);
[Link]("enter first number");
int number1=[Link]();
[Link]("enter second number");
int number2=[Link]();
[Link]("enter choice number");
int choice=[Link]();
int result;
switch (choice) {
case 1:
result=number1+number2;
[Link]("the sum is"+result);
case 2:
result=number1-number2;
[Link]("the difference is"+result);
case 3:
result=number1*number2;
[Link]("the product is"+result);
}
}
}
[Link] is constructor?
a)constructor is a block of code similar to the method.
It is called when object is created.
Ex:Object ob=new Object();
**we are not creating define constructor but we are
calling. That means java will create default constructor.
It is called as implicit constructor.
**otherwise we are creating constructor in a class that
is called explicit constructor.

[Link] for constructor:


import [Link];

public class Object{


int i;
int j;
public Object() {
i=20;
j=10;
}
public Object(int i,int j) {
this.i=i;
this.j=j;
}

public static void main(String[] args) {


Object Ob=new Object();
Object Ob1=new Object(24,24);
Object Ob2=new Object(24,27);

[Link]([Link]());
[Link]([Link]());
[Link]([Link]());

}
public int add(){
return i+j;
}
}
[Link] types of comments?
a)[Link] line comments
[Link] line comments.
[Link] comments.

****Shortcut for single line comments:


Ctrl+7
****Shortcut for multi line comments:
Ctrl+shift+/
****Shortcut for documented comments:
Shift+alt+j
[Link] is looping statements and its types.
a)Definition: looping statments is a statements execute
one or more statements repeatedly several number of
times.
Types:
[Link]
[Link]-while
[Link]
[Link]
1..program for while loop.
Public class Loop{
Public static void main(String[] args){
While(i<10){
[Link](“i”);
i++;
}
}
}
**Program for two dimensional array to finding sum:
public class Arrays {

public static void main(String[] args) {


int[][]arr=new int[3][3];
arr[0]=new int[] {1,8,4};
arr[1]=new int[] {9,7,2};
arr[2]=new int[] {7,6,4};
int sum=0;
for(int i=0;i<[Link];i++) {
for(int j=0;j<arr[i].length;j++) {
sum+=arr[i][j];

}
[Link]("sum of all elements "
+ sum)
}
}
}
**program for single dimensional array.
public class Arrays {

public static void main(String[] args) {


int[]arr=new int[3];
arr[0]=5;
arr[1]=8;
arr[2]=4;

for(int i=0;i<[Link];i++) {
[Link](arr[i]);

}
}
}

**program for for-each loop:


public class Loop{
public static void main(String[] args) {
int[]arr= {1,2,7,8,2,3,4,1};
for(int value:arr) {
[Link](value);
}
}
}
Program for for each using two dimensional array:
public class Loop{
public static void main(String[] args) {
int[][]arr= {{2,4,5},{2,7,3},{1,3,5}};
int sum=0;
int noofelements=0;
for(int[] array:arr) {
for(int value:array) {
sum+=value;
noofelements++;
}

}
[Link](sum);
[Link](sum/noofelements);
}
}
[Link] is String:
String is a sequence of character and it is a class in java
but it considered as a literal.
**it is used to store “text”.
*string is non primitive data type.
Different ways to create string:
[Link] s1=”nandu”;
[Link] s2=new String(“hello”);

Assignment:
**program for to count the number of characters in a
string.
package string;

import [Link];

public class Strings {

public static void main(String[] args) {


Scanner scanner=new Scanner([Link]);
[Link]("enter a word");
String s1=[Link]();
[Link]([Link]());
[Link]([Link]());
[Link]([Link]());
}

}
[Link] is static or non-static::

*normally allocation of memory two types they are:


[Link] creation.
*it takes more space in memory. when each object
creation separate memory is allocated.
[Link]
**it has shared memory.
[Link] is oops?
*oops means object oriented programming system.
Principles of oops:
[Link].
[Link].
[Link].
[Link].
Programming language that follow oops:
1.c
2.c++
[Link]
[Link]:it is a first object oriented programming
language.
[Link]:-it is a truly object oriented programming
language.
****[Link]:Inheritance in java means aquires
properties from one class to the another class.
Program:-
Public class Admin extends Developer{
Public void read{
[Link](“read code”);
}
Public void Write{
[Link](“read code”);
}
Public code Manage{
[Link](“manage code”);
}
}
Program:
Public class Admin Extends Developer{
Public void manage(){
[Link](“manage code”);
}
}

}
Encapsulation:
Encapsulation means binding code and data together in a single unit called encapsulation.
Program :-
public class Student {

int rollnumber;
String name;
boolean Attendence;
public Student() {
[Link] = 502;
}

public boolean isAttendence() {


[Link]("teacher assigned attendence");
return Attendence;

public void setAttendence(boolean attendence) {


Attendence = attendence;
[Link]("teacher accessed student attendence");

}
}

public class Teacher {


public static void main(String[] args) {
Student stu=new Student();
[Link]=true;
//[Link](true);
[Link]();

[Link](true);
//[Link];

[Link] is access modifiers and its types?


a)Definition:the access modifiers is a scope of the
members of a class.
Types:
[Link] access modifiers.
2. private access modifiers.

[Link] access modifiers.


4. Default access modifiers.
[Link] is an interface?
A)An interface is the blueprint of a class.
*if you we want to implement interface use keyword
implements.
Program:
Public class Laptop{
Public void copy();
Public void paste();
Public void cut();
Public void camera();
}
Public class Lenovo{
Public void copy{
[Link](“Lenovo copy code”);
}
public void paste{
[Link](“Lenovo paste code”);
}
Public void cut{
[Link](“Lenovo cut code”);
}
Public void camera{
[Link](“Lenovo camera code”);
}
}

Public class user{


Public static void main(String[] args){
Lenovo Lenovo=new Lenovo();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
23)abstract class:
a)abstract class extends class with abstract name.
program:

24)what is abstraction?
a)Abstraction means hiding internal details and
shoeing their functionality.
*through interface we achieve abstraction 100%
before java 1.8 version.
*After 1.8 version may or may not be we achieve 100%
abstraction.
25)What is polymorphism?
A)Ability of an object to take many forms called
polymorphism.
Different types of polymorphism:
1)compile-time polymorphism.
2)Run-time polymorphism.
1)compile -time polymorphism is a type of
[Link] will occur in the compile time.
**it is also referred as static polymorphism.
**compile time polymorphism achieved by the method
overloading concept in java.
2)Run-time polymorphism:
Run-time polymorphism is a type of [Link]
will occur in the Run time.
**it is also referred as dynamic polymorphism.
**Run- time polymorphism achieved by the method
overriding concept in java.
Method overloading:
Method overloading means that allows multiple
methods but different in parameters.
Ex:
[Link]();
[Link]();
[Link]();
Method overriding:
Method overriding means when a child provides anew
implementation that already defined in theparentclass.

26)What is an Exception?
A)Exception is an event that occur during the execution
of a program that distrupts the normal flow of an
instruction.
27)What is Exception Handling?
A)If an exception occurs means the java stop the
execution. that is handled by ourself means Exception
handling.
Different types of Exception:
1)checked-Exceptions.
2)unchecked-Exceptions.
28))Files in the java?
[Link] path.
[Link] path.
Program:
Public class Files{
Public static void main(String[] args){
File f=new File(“./[Link]”);
If(![Link]())
[Link]();
FileInputStream fis=new FileInputStream(f);
Int asciicode;
While(asciicode=[Link]())!=-1){
[Link]((char)asciicode);
}
[Link];
}
}
29))What are the blocks?
a)
[Link]-catch
[Link].
[Link]
[Link]
30)What is pojo and java bean?
a))*pojo means plain old java object.
*It is a simple java object ,which is not bounded by any
special restrictions.
Rules for a class to be called as pojo-class:--
 it must be public.
 It should not implement any interface.
 It should not extend any classes.
 It should not have any annotations specified.
 It is recommended to make the properties as
private to improve security.
 It can have getters & setters in order to access the
properties.
Rules for a class to be called as a java bean class:--
 It should implements with serializable interface.
 It should have no-args constructor.
 All the properties should be private.
 It should have getters &setters in order to access
the properties.
31)What is the vector and the ArrayList?
a)Vector: Vector is a re-sizable array.
Major operations in both arrays and the Arraylist:--
 Creation of a list.
 Addition in a list.
 Deletion in a list.
 Updation in a list.
 Retriving from a list.
 Verification in a list.

Introduced :
 Vector introduced in 1.0 version.
 Collection introduced in 1.2 version.
 Generics introduced in 1.5 version.
Vector(important points):--
*100% increment occurs array size.
Ex:
10
20
40
 The default capacity is 10.
 The initial capacity is 10.
 It allow duplicate values.
 It allow null values.
 It maintains the insertion order.
 It does not maintain sorted order.
 It follow synchronisation.
 It is good at multithreading, data increased
exponentially.
Arraylist(important points):--
*50% increment occurs array size.
Ex:
10
15
20

 The default capacity is 0.


 The initial capacity is 10.
 It allow duplicate values.
 It allow null values.
 It maintains the insertion order.
 It does not maintain sorted order.
 It not follow synchronisation.
Stack(important points):--
*100% increment occurs array size.
Ex:
10
20
40

 The default capacity is 10.


 The initial capacity is 10.
 It allow duplicate values.
 It allow null values.
 It maintains the insertion order.
 It does not maintain sorted order.
 It follow synchronisation.
 It is used in undo,redoetc.

Common questions

Powered by AI

Programming is a way to instruct computers to perform tasks in a specified manner, allowing for automation and efficient execution of complex operations. In today's world, where much of our environment is automated, programming facilitates this automation and the interaction between humans and machines, making processes faster and more efficient .

The main principles of Object-Oriented Programming (OOP) are inheritance, encapsulation, polymorphism, and abstraction. Inheritance allows for code reusability by inheriting properties and behaviors from other classes. Encapsulation refers to hiding the internal state and requiring interaction through methods, enhancing security and modularity. Polymorphism enables methods to perform different tasks based on objects, enhancing flexibility and integration. Abstraction involves hiding complex implementation details and exposing only necessary functionalities, simplifying interface usage. Together, these principles promote organized, reusable, and maintainable software design .

Imperative programming is focused on describing how a program operates, with programmers defining explicit sequences of commands or statements to change the program's state. It includes subtypes like functional programming and object-oriented programming (OOPs). Declarative programming, on the other hand, emphasizes the 'what' of a program's operations, focusing on the desired results without specifying the sequence of commands, and includes structural and logical programming. These paradigms influence programming practices by shaping how developers approach problem-solving and code organization .

Method overloading in Java is a form of compile-time polymorphism that allows methods in the same class to have the same name but different parameters. This offers flexibility and cleaner code by providing variations of the same operation. Method overriding, a form of runtime polymorphism, allows a child class to provide a specific implementation for a method already defined in its parent class. This enables dynamic method invocation, allowing behavior modification without altering method signatures. Both techniques enhance extensibility and adaptability in Java programs .

Java, as an object-oriented programming language, offers several advantages: it is simple, platform-independent, secure, and robust. These make Java particularly suitable for developing a wide array of applications, from desktop to web-based. However, it also has disadvantages, such as slower performance compared to C++ and less effective user interface capabilities. C++ might be preferred in scenarios where performance is critical, but Java is favored for its cross-platform capabilities and security .

Access modifiers in Java are keywords that define the visibility scope of classes, methods, and other members. The main types include public, private, protected, and default. Public members are accessible from anywhere, enabling broad usage. Private members are restricted to within their own class, protecting sensitive data. Protected members are accessible within their package and subclasses, balancing accessibility with security. Default members, accessible only within their package, provide a baseline of encapsulation. These modifiers control how an API is exposed and ensure secure and modular code .

Java manages exceptions by allowing developers to define exception-handling blocks that catch runtime errors without crashing the program. The try-catch construct lets you catch and define how to handle specific exceptions thrown in try blocks. The finally block executes cleanup code after try-catch, ensuring resource management. The throw keyword triggers an exception explicitly, while throws in method signatures indicate potential exceptions callers must handle. These mechanisms provide robust error detection, reporting, and recovery, essential for building fault-tolerant applications .

Vector and ArrayList in Java both allow dynamic resizing and support duplicate elements. However, their implementations differ: Vectors are synchronized, making them thread-safe but potentially slower due to overhead, whereas ArrayLists are not synchronized, offering better performance in single-threaded environments. Vectors increment capacity by 100% when resized, compared to 50% for ArrayLists, affecting memory allocation strategies. These differences influence their usage based on performance and threading requirements, with Vectors being more suitable for concurrent applications and ArrayLists preferred for general use .

In Java, instance variables are unique to each instance of a class, thus affecting memory allocation on a per-object basis. Class variables, or static variables, share a single copy across all instances, leading to shared memory usage and potentially improved performance in scenarios with many instances. Local variables are declared within methods and are temporary, with minimal impact on memory beyond their scope. Parameters are variables passed into methods and share similar characteristics with local variables. Balancing these types leads to efficient memory management and optimized program performance .

Constructors in Java are special methods called when an object is instantiated. They initialize the object's variables and can define object behavior upon creation. Implicit constructors are provided by Java when no explicit constructor is defined, ensuring that an object is always initialized. Explicit constructors are defined by developers to control initialization details, allowing customization of object states and behaviors. This affects how classes encapsulate initialization logic and flexibility in object creation .

You might also like