×

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

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Class vs Object in Java

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Java String contains() method

contains() method returns true only if it contains the given sequence of characters otherwise it returns false. syntax: public boolean contains(CharSequence sequence) parameters: sequence : It is the sequence to be searched. Returns: It returns true...

1 minute read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

Dictionary in Java

Dictionary in Java The Dictionary class represents a key-value relation which maps keys to values. In Dictionary class, every key and every value is an object. It is an abstract class associated with...

2 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.

Java Return Keyword

The return keyword in Java is used to end a method's execution. the caller receives the return, followed by the appropriate value. The return type of the method, such as...

3 minutes read.

Java Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

Java Math abs() Method

The abs() method of Math class returns the absolute value of the argument where the argument can be int, double, float, long. Syntax public static int abs(int a) public static float abs(float a) public...

2 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

Java Set Interface

We use set when we don't want to allow duplicate entries. All Set implementations do not allow duplicates. HashSet: This class stores its elements in hash tables. It uses the hashCode()...

3 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

3 minutes read.

Counting sort in Java

An array's elements are sorted using the counting sort method, which counts how many times each distinct element appears in the array. The count is kept in an auxiliary array,...

3 minutes read.

ArrayList Program in Java

ArrayList Program in Java: In Java, ArrayList is a class that belongs to java.util package. It is the dynamic list that grows or shrinks at run-time as per the requirements....

4 minutes read.

Java Byte intValue() method

The intValue()  method of Java Integer class returns an int value for this Integer.  Syntax public int intValue()  Parameters NA  Specified by This method is specified by intValue in class Number  Return Value This method returns the numeric...

1 minute read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Java String replaceFirst() method

This method replace the first substring with user specified String. Syntax: public String replaceFirst(String Regexp, String Replacement) Parameter: Regexp : Regular Expression Replacement : the String which would replace found expression Return: a string Java String replaceFirst()...

1 minute read.