What is native machine code in java

What is native code in Java

The interpreter or Java Virtual Machine (JVM) converts byte code to native code in Java.

However, before that the compiler produces byte code.

As a result, any computer that has Java Runtime Environment or JRE, can run that Java program.

Here comes our problem five or code five. Consider the problem first and see the output.

//problem 5 //code 5 package fun.sanjibsinha; public class NewClass < public static void main(String[] args)< byte numOne = 10; byte numTwo = 12; byte sumOfTwoNumbers = 0; sumOfTwoNumbers = numOne + numTwo; System.out.println(sumOfTwoNumbers); >>

In the output we have got an error, like this:

//output of problem 5 Error:(8, 34) java: incompatible types: possible lossy conversion from int to byte

We need to find why this happens to us?

As you know, “byte” is the smallest integer data type.

You can assign a byte variable a range of integer values between (-128) to 127.

However, byte has no literals or constants like “int” or “long”.

It means you cannot assign an integer constant expression to a byte variable.

Another possible explanation that comes to my mind tells us that in Java, the int type is coded using 32 bits and the byte type using 8 bits.

However, it finally gives output in the native code in Java.

  • If you are a programming beginner you may take an interest in the following posts.
  • Steps in program development
  • Learn Programming Techniques
  • The levels of programming languages
  • What is high level language?
  • What is language portability?
  • Programming languages translators
  • Learn structured programming
  • Machine language to Assembly language
Читайте также:  Создать массив python заполненный нулями

If you convert an int to a byte, there is a chance that you may lose information.

Yet we can do the same operations using the reference variables. We will discuss that topic later.

In the above program, when we add two byte variables, it becomes an int literal expression.

You cannot equate that with a byte variable anymore. We cannot convert int constant to byte now.

We can solve this problem by two methods. The first solution is here.

//problem 6 //code 6 package fun.sanjibsinha; public class NewClass < public static void main(String[] args)< byte numOne = 10; byte numTwo = 12; byte sumOfTwoNumbers = 0; sumOfTwoNumbers = (byte)(numOne + numTwo); System.out.println(sumOfTwoNumbers); >>

And there is no error anymore.

//output of problem 6 /usr/lib/jvm/jdk12.0.1/bin/java -javaagent:/snap/intellij-idea-community/185/lib/idea_rt.jar=37275:/snap/intellij-idea-community/185/bin -Dfile.encoding=UTF-8 -classpath /home/ss/IdeaProjects/HundredProblemsInJava/out/production/HundredProblemsInJava fun.sanjibsinha.NewClass 22 Process finished with exit code 0

The addition of two numbers is not greater than 127. So if we can convert that integer literal expression to byte by this line:

sumOfTwoNumbers = (byte)(numOne + numTwo);

Now the integer literal expression is byte and the value is within the range.

Furthermore, we can solve this problem with another trick, where we can declare the variable “sumOfTwoNumbers” as an integer.

Consider the following code.

//problem 7 //code 7 package fun.sanjibsinha; public class NewClass < public static void main(String[] args)< byte numOne = 10; byte numTwo = 12; int sumOfTwoNumbers = 0; sumOfTwoNumbers = numOne + numTwo; System.out.println(sumOfTwoNumbers); >>

Here the output is same as before.

//output of problem 7 /usr/lib/jvm/jdk12.0.1/bin/java -javaagent:/snap/intellij-idea-community/185/lib/idea_rt.jar=34621:/snap/intellij-idea-community/185/bin -Dfile.encoding=UTF-8 -classpath /home/ss/IdeaProjects/HundredProblemsInJava/out/production/HundredProblemsInJava fun.sanjibsinha.NewClass 22 Process finished with exit code 0

Here in the above solution, the trick lies in this line:

As a beginner, it may seem a little bit difficult for you to understand how these data types work.

Читайте также:  Arithmetic operators python решение

So, next, we will discuss this topic along with object-oriented-programming concepts, so that after finishing chapter four you can start writing some simple code snippets on your own.

Источник

Оцените статью