In this tutorial, we will learn about the new keyword in Java programming language
new is a keyword in Java which is used to create a Java object and allocate memory space in memory disk. new is also used for array creation.
new keyword is used in many ways in Java.
syntax
The syntax of new keyword of array declaration
<data_type> <array_name>=new <datatype>[size];
Example
Example of array declaration using new keyword
int marks=new int[5]; String [][] stu_name=new String[5][10];
Syntax
<class_name> <object_name>=new <class_name>
Example
class school{ } class student{
school obj=new school();
//this is an object for school class }
Example
Program 1
program for using new keyword in Array
class new_keyword1{ public static void main(String args[]){ int number[]=new int[4]; //Array declaration used bew keyword System.out.println("Value of index number[0] ="+number[0]); System.out.println("Value of index number[1] ="+number[1]); System.out.println("Value of index number[2] ="+number[2]); System.out.println("Value of index number[3] ="+number[3]); //display element of every cells of array } }
When the above code is compiled and executed, it will produce the following results
Value of index number[0]= 0 Value of index number[1]= 0 Value of index number[2]= 0 Value of index number[3]= 0
Program 2
program for using new keyword in create object
class Stu_marks{ int marks=78; public static void main(String args[]){ Stu_marks stu=new Stu_marks(); System.out.println("Student marks :"+stu.marks); } }
When the above code is compiled and executed, it will produce the following results
student marks :78
New keyword is used in Scanner class for memory allocation when create Scanner object
Example
Scanner scan=new Scanner(System.in); //predefined standed input object
There are other Java language keywords that are similar to this keyword
Usage of this statement in the Java language
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…
View Comments
F*ckin?remarkable things here. I抦 very glad to see your post. Thanks a lot and i am looking forward to contact you. Will you please drop me a mail?