×

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It has a 32-bit signed; two's complement integer storage capacity. The keyword "int" is used to store an integer value in Java programming. Methods with a return value can be created using int. In Java computer languages, it is a primitive data type.

Important points of int keyword in java:

  • The integer that is initiated can be any value between -231 and 231, inclusive.
  • Int is a 32-bit unsigned integer with a minimum value of 0 and a maximum value of 232-1 that can be used as of Java 8.
  • The default setting of the java int keywordis allocated by 0.
  • The java int keyword stores a size of4-byte which is a default size.
  • For integral values, int is typically used as a default data type.
  • In Java, the int has a wrapper class called Integer.
  • While an integer may store a null value, an int cannot.
  • In comparison to primitive type, the integer class has the advantage of being usable in collections with generics or object references.
  • Like other classes, the integer class requires that you build objects with a single field, called int.

Syntax

The syntax of keyword int is:

int <variable_name><integer_value>;

Examples of Java int Keyword

Example 1:

In this program we will implement the int keyword to print positive and negative values.

public class IntEx1  
{  
public static void main (String...s)  
{  
int H1=20;
int H2=-30;
System.out.println("H1: "+H1);
System.out.println("H2: "+H2);
}  
}  

Output:

H1: 20
H2: -30

Example 2:

This program is to check whether the int keyword stores the decimal values or not.

public class IntEx2  
{  
public static void main (String...s)  
{  
int hi=11.5;
System.out.println("hi: "+hi);
}  
}  

Output:

error: incompatible types: possible lossy conversion from double to int.

Example 3:

This program is to check whether the int keyword stores the float values or not.

public class IntEx3  
{  
public static void main (String...k)  
{  
int n=20f;
System.out.println("n: "+n);
}  
}  

Output:

error: incompatible types: possible lossy conversion from float to int

Example 4:

Let's look at an example to see if the int data type can store char values. In this situation, the compiler implicitly converts the character to an int type and outputs the corresponding ASCII value.

public class IntEx4  
{  
public static void main (String...s)  
{  
int n11='c';
System.out.println("n11: "+n11);
}  
}  

Output:

n11: 99

Example 5:

The minimum and maximum values in this example are stored in the int data type.

public class IntEx5  
{  
public static void main (String... s)  
{  
int low value=-2147483648;
int high value=2147483647;
System.out.println("low value: "+low value);
System.out.println("high value: "+high value);
}  
}  

Output:

low value: -2147483648
high value: 2147483647

Example 6:

This program is to create the method which returns the value of integer.

public class IntEx6 {  
    public int output (int n)  
    {     
        return n;
    }  


    public static void main (String [] s) {  


        IntEx6 bj=new IntEx6();
        System.out.println(bj. Output (30));
    }  


}  

Output:

30

Example 7:

This program is using methods and liocal variables.

public class LocalVariableEx 7 {
    public int add (int a) {
        int add = 0;
        for (int i = 0; i <a; i++) {
add = add + i;
        }
        return add;
    }


    public static void main (String [] s) {
        LocalVariableEx 7 localVariableEx 7 = new LocalVariableEx 7();
        int sum = localVariableEx 7.add (10);
        System.out.println("addition of initial 10 numbers -> " + add);
    }
}

Output:

addotion of initial 10 numbers -> 45

Example 8:

public class intEx 8{
    public static void main (String s []) {
boy age (18);
    }
            static int boy age (int a) {
        System.out.println("Boy age is :"+a);
return0;
}
} 

Output

Boy age is: 18

Related Topics

Java Math cosh() Method

The cosh() method of Math class returns the first hyperbolic cosine((e+e)/2) of a double value. Syntax: public static double cosh(double x) Parameters: The parameter ‘x’ represents the number whose hyperbolic cosine is to be...

2 minutes read.

Tug of War in Java

As in the tug-of-war issue, we must divide the given collection of n numbers into two groups of sizes that are equal or nearly equivalent. A minimum difference must exist...

5 minutes read.

Creation of Multi Thread in java

What are threads in Java? We can use threads to facilitate parallel processing. Threads are helpful when you wish to execute several pieces of code concurrently. A thread is a small process...

3 minutes read.

How to Compare Two Strings in Java

How to Compare Two Strings in Java On the basis of reference or content, one can compare two strings in Java. String comparison is used in reference matching (== operator), sorting...

4 minutes read.

Various operations on the Queue using Stack in Java

The Java Collections Framework's core data structures are the Stack and Queue. They are used to store and retrieve identical data in a presentation sequence. These two linear data structures...

7 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

How to Call a Method in Java

In Java, a method is a collection of statements that perform a specific task or action.It can accept data with the help ofitsarguments. It  is also called a function. In order...

9 minutes read.

Minimum Lights to Activate Java Snippet Class

Minimum Lights to Activate Problem in Java In prison, there is a hallway that is N units long. Given an N-dimensional array A. If the light at the ith position is...

3 minutes read.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Interface Program in Java

Interface Program in Java In the previous topic, we discussed that abstraction is possible through the interface and abstract class. An abstract class provides partial to 100% abstraction. 100 %abstraction in...

4 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

Java Single Linkedlist

Like arrays, linked lists are a type of linear data structure. Unlike arrays, which store each element in the same location, linked lists employ pointers to connect the elements together. In...

25 minutes read.

Gregorian Calendar Java Current Date

GregorianCalendar class uses the Gregorian and Julian calendars. Dates are calculated by projecting present laws forever backward and forward in time. As a consequence, GregorianCalendar may be utilised to create...

8 minutes read.

How to calculate time complexity of any program in Java

Java : Java's syntax and principles are derived from the C and C++ languages. We know that java is one of the programming language. The main feature of java which is...

3 minutes read.

Get yesterdays date by no of days in Java

In this tutorial, we are going to learn how to get yesterday’s date by the no of days in Java. Using the Calendar class, one can get the current date....

1 minute read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.