In this blog, we will be going to learn all the essential concepts of java programming from top to bottom (i.e learn java programming in one blog). After reading this blog you will be able to understand how java programs work and what is object-oriented concepts in java. So let’s start.
Before jumping into learning I am assuming that people having eagerness to learn programming is sufficient for this blog and no programming background is needed.
Whenever we are jumping into the programming world some sort of question comes into mind which programming language we should choose and how much time it will take to learn? So whenever this type of question comes to mind then definitely go check out some videos and try to analyze in which area you want to grow further. But at the start, I will suggest you go with the java programming language because after learning some core java concepts and doing programming practice in that area then you will be able to switch between other programming languages becomes easier. And note every programming language has its own Pros and Cons. So Choose one programming language at the start and mostly 3 months are sufficient at the start to learn all the concepts with some sort of practice. After reading this blog it will be easier for you to apply all that knowledge to understand java code is sufficient but as you know practice makes a man perfect in a particular area.
//double slash is used for single comment.
/*
double slash with 2 star in it is used for multi-line comment and note comment is used just for reference it is not compiled by complier.
*/
/*
Things to note about java:-
-Java is compiler based language in which whole code is compiled at once and converted into bytecode with ClassName.class extension then output is given
-java file is saved with "ClassName.java" otherwise it will give runtime error.
*/
// "class" is a predefined keyword in java used to create class in java which results in Encapsulation i.e binding all the codes together or function at one //place and "ClassName" is your class name which you define and follow camel case convention with first letter capital.
class ClassName{
//public is access modifier and static which enables us to do things without creating an instance for an Object in java and "void" is the return type.
// main(String args[]) -- main is the main method in which is executed at start and contain array of string arguments.
public static void main(String args[]){
System.out.println("Hi this is the sample body of java program");
}
}
ouput:-
Hi this is the sample body of java program
Note:- In Java, Each Statement Ends with a semicolon that why java is called a strictly typed language.
- How to print in java:
- There is mainly two three forms of a printing method in java each discussed below.
- System.out.println(“Hi all, I am learning learning coding with codewithdc.com”);
- output:- Hi all, I am learning coding with codwithdc.com
- note we can concatenate some variables with the string and print it together.
- System.out.printf(“%s %d”, “codewithdc.com”,4);
- output:- codewithdc.com 4
- where “%s” is used for printing strings and “%d” is used for printing integer data type
- Buffereader method
- Buffereader is the method used for input as well as for output the string
- System.out.println(“Hi all, I am learning learning coding with codewithdc.com”);
- There is mainly two three forms of a printing method in java each discussed below.
- Variables in java:
- variables in java can be declared using Identifier following data type let’s discuss each data type below.
- integer:
- int variableName1, variableName2, variableNam3;
- variableName1 = 2;
- note if you have declared one variable then cannot it again with any datatype.
- String:
- String myWebsite = “www.codewithdc.com”;
- note first letter of the String keyword is capitalized in java because String below Object data type in java
- Boolean:
- boolean myWebsiteIsGood = true
- boolean codingNeedsLessTime = false
- true and false are reserved keywords in java.
- float:
- float myPercentage = 99.99f
- note assigning value should end with f which denotes float in java
- double
- double absoluteValue = 12.3333333d
- note assigning value ends with d.
- long:
- long biggervalue = 123235345234263345623452345;
- char
- char firstLetter = “A”
- integer:
- variables in java can be declared using Identifier following data type let’s discuss each data type below.
- Functions In Java:
- functions in java can be declared using a combination of access modifiers, and return types.
- static int addTwoNo(int a, int b){ return a+b;}
- static: it denotes that the method declared inside the main class, not in the derived ones
- int is the return type in a method
- addTwoNo(int a, int b): it is the method name or method identifier that takes some parameters in it like a and b which is of integer datatype.
- To call the above function we can do it as shown with some parameters.
- addTwoNo(1, 2)
- note we can store functions value in variables as well.
- int a = addTwoNo(1,2)
- static int addTwoNo(int a, int b){ return a+b;}
- functions in java can be declared using a combination of access modifiers, and return types.
class Calculation{
static int addTwoNo(int a, int b){
return a+b; // 1+2 = 3 will be returned
}
public static void main(String args[]){
int a = addTwoNo(1,2); //storing return value of addTwoNo(1,2) in variable a
System.out.println(“added value is:”+a);
}
}
4. Exception Handling In Java:
- As we know whenever we are doing things there are always exceptional cases that come into the picture like in real-life scenarios, for example, we are traveling in a train and TC comes in front of us and asks for a ticket at that time we thought that I forgot ticket at home and hence this is the thing which happened unexpectedly.
- So to handle such a type of exception in the programming world java provides some method to handle it in an easy manner. So let’s discuss each in below code block.
- try { some statement;}
- try is a predefined keyword in java that is used to monitor the code inside the try block so that if exceptions occur it will be handled by the “catch block”.
- catch e { some statement;}
- the catch is a predefined keyword in java that is used to catch exceptions thrown in the try block and we can specify exceptions like ZeroDivisionError OR any exception as e in this case after the catch keyword.
- try { some statement;}
5. Object Oriented Programming In Java:
- Object Oriented Programming In Java or OOPS concept in java means following Inheritance, Polymorphism, Abstraction, Encapsulation properties while programming in java and it is important to do programming in java using this property because it enables our code reusable, remarkable, hide inner part of programming, bind our code together. So let’s discuss each below.
- Inheritance:
- In java any subclass can inherit property from its parent class is known as inheritance
- To create a subclass of any class we use extends keyword in java.
- class A{ some statement;}
- class B extends A{ some statement;}
- Now B will be inheriting all the properties of an A but the reverse is not possible.
- Encapsulation:
- In java, encapsulation means binding all the code together in one place like using the class keyword with the ClassName we are storing and the inner statements and methods/functions in one place.
- Abstraction:
- In Java, abstraction means hiding the implementation of code and showing only necessary things to the users.
- for example, a Car looks good from outwards but if we look inside the car i.e their engine connection then it will look very complex for every user so hiding implementation and providing necessary things are mandatory in the real world here user will be know how to start the car or how to ride, turn left/right, change gear, etc.
- Polymorphism:
- In java, polymorphism is the concept from which we can make many forms of particular methods/functions in java.
- static int addNumbers(int a, int b) // here this method is accepting only two integers
- static int addNumbers(int a, int b, int c) // here this method is accepting three integers with the same name addNumbers i.e polymorphism
- Inheritance: