Java println() of integers? [duplicate] - java

This question already has answers here:
How to concatenate int values in java?
(22 answers)
Closed 7 years ago.
I want to print the two integer variables divided.
int a = 1, b = 2;
System.out.println(a + b);
Obviously println() function processes them as integers and calculates the sum.
Instead I would like the output becomes like this "12". Any ideas?

Insert some blank Strings to induce this:
System.out.println(a +""+ b);

Store the integers as strings:
String a = "1";
String b = "2";
String c = a + b;
System.out.println(c);

Related

Double Literally Result False [duplicate]

This question already has answers here:
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
How can I properly compare two Integers in Java?
(10 answers)
Closed 3 years ago.
My Question about Wrapper classes. Case 1: I'm Declaring two Integer variables and assigning same values 127, and i'm printing both hashCode. It's Generated same hashCode and result will be printed true. But In Case 2: Generated the Same hashCode but result will be printing false. Please Could you relate. Why Printing a false result.
package com.oca.test.exam;
public class TestCasesIntegerAndDouble {
public static void main(String[] args) {
//Case 1:
Integer i1 = 127;
Integer i2 = 127;
System.out.println(i1.hashCode()); //127
System.out.println(i2.hashCode()); //127
System.out.println(i1 == i2); //true
//Case 2:
Double d1 = 127.0;
Double d2 = 127.0;
System.out.println(d1.hashCode());//1080016896
System.out.println(d2.hashCode());//1080016896
System.out.println(d1 == d2); //false
}
}

Data type of Literals/Result of Arithmetic operations on Literals in Java [duplicate]

This question already has answers here:
Java: Arithmetic operation on byte
(3 answers)
Why can not I add two bytes and get an int and I can add two final bytes get a byte?
(3 answers)
Closed 5 years ago.
Can anybody please clarify the below:
byte a = 10 + 20; // b=30;
-------------
byte b=10,c=20;
byte a = b + c; //error. Casting required.
What was the data type of result of addition in the first line before assigning it to 'a'? Do literals have a data type. Or do arithmetic result assign them one ?
since b + c is an operation that can overflow you need to do
byte a = (byte)(b + c);

Fixing Disappearing Zeros [duplicate]

This question already has answers here:
How can I pad an integer with zeros on the left?
(18 answers)
Closed 6 years ago.
I wanted to divide the integer and store it in an array
For Ex:1000000000000 into two indexes
arr[0]=1000000
arr[1]=000000
but arr[1] stores it as 0 instead of 0000000.
I wanted to perform some operations with it,so i needed 7 zeros in it ,instead of 1 zero.
Is it achievable in some way ?
Convert your number to a string then use substring() to split it up.
long num = 1000000000000L;
String str = num + "";
String[] array = new String[2];
array[0] = str.substring(0, 6);
array[1] = str.substring(7);

Java equals plus operator [duplicate]

This question already has answers here:
What is the difference between a += b and a =+ b , also a++ and ++a?
(9 answers)
Closed 8 years ago.
I need to explain this strange operator =+ (equal plus)
Example #1:
Double a = new Double(5);
Double b = new Double(10);
a += b
result:
a=15.0
b=10.0
Example #2:
Double a = new Double(5);
Double b = new Double(10);
a =+ b
result:
a=10.0
b=10.0
I understand the first example, but please explain me what this =+ operator did in example no.2.
And another interesting fact is, that these operators are valid and compilable:
+=, -=, *=, /=
but any of these two won't compile:
=*, =/
=+ is the assignment operation and the unary + afterwards. It's perfectly valid and what happens is:
a = (+b);
It's pretty much the same when you want to assign the negative value of a variable to another variable:
a = (-b); //a will be assigned with -10
Also, =* doesn't compile, because there's no * unary operator.

What is ++int in Java? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
explain working of post and pre increment operator in Java
What is the difference between int++ and ++int?
In Java, what is ++int? What does it do? What does it mean?
(Sorry, but I didn't ask the question correctly last time.)
a = 5; b = ++a; // a = 6, b = 6
a = 5; b = a++; // a = 6, b = 5
int a=1;
System.out.println(a++);
prints "1"
int a=1;
System.out.println(++a);
prints "2"
Or maybe i don't understand your question.
++int increments int by 1 before and int++ increments by one after

Categories

Resources