FAQ Database Discussion Community


java - StringUtils.join() returning pointer

java,types,return-value,primitive
I was trying to join the elements of an array via the StringUtils.join(array, separator) method from the Apache commons library. Can anyone please explain me why I cannot get the resulting string and only a pointer to its location if I use an array of primitive types like int[] ?...

Is it more expensive to swap objects or primitives in java?

java,object,primitive
Say I have an array of some comparable objects. For convenience, think Integer. But really it could be some made up data type such as Car or Stuff. Say further that I need to find the extremum. Is it cheaper to swap the indices? the objects? or it does not...

Using Special Characters in Java (using primitive char)

java,arrays,char,special-characters,primitive
I'm basically trying to build a random password generator in Java. I'm trying to build it using a char array, basically: char[] password = new char[8] I want to be able to include "special" characters in this array (such as @, $, *, etc). Can these characters be placed in...

What Primitive To Use In Java

java,primitive,primitive-types
I am a little confused on when to use what primitives. If I am defining a number, how do I know what to use byte, short, int, or long? I know they are different bytes, but does that mean that I can only use one of them for a certain...

F# cast / convert custom type to primitive

types,casting,f#,primitive
I've designed my app domain with custom F# types, but now it seems like these custom types will be a PITA when I want to actually use the data for various tasks... i.e. writing values to a CSV file, using other libraries that rely on primitives, etc. For example, I...

How does primitive value conversion happen in `1 + {}` and `{} + 1`?

javascript,primitive
I'm a beginner developer, and I can't understand why below statements have such outputs. Can someone explain that how/why following two expressions are interpenetrated by JavaScript differently, that their outputs are different. 1 + {} // => "1[object Object]" {} + 1 // => 1 As + is a commutative...

passing primitive int array class (int[].class) to a generic method which expects Class fails to compile

java,arrays,generics,primitive
I have following code: public class Test { private Map<String, Object> delegate; public Test(Map<String, Object> delegate) { this.delegate = delegate; } public <T> T[] getValueAsArray(String key, Class<T[]> arrayClz) { T[] ret = null; Object val = delegate.get(key); if (val != null && arrayClz != null && arrayClz.isInstance(val)) { ret =...

Equality of boxed boolean

java,primitive,autoboxing
Quick question: is it guaranteed that this code always prints true? Boolean b1 = true; Boolean b2 = true; System.out.println(b1 == b2); Boxing of boolean seems to result in the same Boolean object all the time, but I couldn't find too much info about boxed Boolean equality in JLS. On...

How does Array treat primitive data types as objects?

java,arrays,object,primitive
I am a bit new to Java Programming. Two or three days ago, I encountered a Question regarding arrays in my mind which is give below. Every Java Programmers knows, Array is a collection of Objects, it doesn't matter whether it contains primitive data types or Strings. So my Question...

String Contatenation with primitives in java

java,string,variables,concatenation,primitive
I just recently started learning Basics in Java, and was testing around initializing a string variable by concatenating primitive variables. public class Main{ public static void main(String[] args){ byte lbyte = 3; short lshort = 1; int lint = 1; long llong = 0; float lfloat = 2.0f; double ldouble...

Java method returns double | int

java,int,double,primitive
I need a method that will accept String number as a parameter and return double if it has a remainder, or int if it is decimal. I wrote such code: private double convertToNumber(String number) { double d = Double.parseDouble(number); if (d % 1.0 == 0) { return Integer.parseInt(number); } else...

How to safely handle Java's wrapped primitives

java,nullpointerexception,primitive,autoboxing
I'm writing a program which needs to handle objects with many wrapped number variables such as Long, Double, Integer etc. How can I safely perform numeric operations on these without having to put null-checks everywhere? I expect this is something that pretty much every Java programmer must deal with sooner...

Java Long.MAX_VALUE property throws “integer number too large” exception! Am including “L” [duplicate]

java,primitive
This question already has an answer here: How to use the unsigned integer in java 8? 3 answers I am using Java SE Development kit 8 update 25(64 bit). I have read the "Primitive Data Types" java tutorial from The Java™ Tutorials and according to this specification the long...

what is the difference between passing an object and a primitive data as the parameter in Java?

java,parameters,primitive
I'm a little bit confused about the difference between passing an object and a primitive data as the parameter in Java. I read a post here explaining that when you pass a primitive data, you copy that data and pass it, but if you pass an object then you pass...

Why isn't BigInteger a primitive

java,biginteger,primitive
If you use BigInteger (or BigDecimal) and want to perform arithmetic on them, you have to use the methods add or subtract, for example. This may sound fine until you realize that this i += d + p + y; would be written like this for a BigInteger: i =...