primitive

Are primitive datatypes thread-safe in Java

穿精又带淫゛_ 提交于 2019-11-27 04:45:13
Are the primitive data types like int & short thread-safe in Java? I have executed the following code and couldn't see expected result 500 some times. public class SampleThree extends Thread { static long wakeUpTime = System.currentTimeMillis() + (1000*20); static int inT; public static void main(String args[]) { System.out.println("initial:" + inT); for(int i=0; i<500; i++) new SampleThree().start(); try { Thread.sleep(wakeUpTime - System.currentTimeMillis() + (1000*30)); System.out.println("o/p:" + inT); } catch(Exception e){ e.printStackTrace(); } } public void run() { try { long s =

Why doesn't Kotlin allow to use lateinit with primitive types?

旧巷老猫 提交于 2019-11-27 04:45:06
In the Kotlin language we, by default, have to initialize each variable when it is introduced. To avoid this, the lateinit keyword can be used. Referring to a lateinit variable before it has been initialized results in a runtime exception. lateinit can not, however, be used with the primitive types. Why is it so? For object types, Kotlin uses the null value to mark that a lateinit property has not been initialized and to throw the appropriate exception when the property is accessed. For primitive types, there is no such value, so there is no way to mark a property as non-initialized and to

Java storing two ints in a long

萝らか妹 提交于 2019-11-27 04:29:39
问题 I want to store two ints in a long (instead of having to create a new Point object every time). Currently, I tried this. It's not working, but I don't know what is wrong with it: // x and y are ints long l = x; l = (l << 32) | y; And I'm getting the int values like so: x = (int) l >> 32; y = (int) l & 0xffffffff; 回答1: y is getting sign-extended in the first snippet, which would overwrite x with -1 whenever y < 0 . In the second snippet, the cast to int is done before the shift, so x actually

What is an int() Called?

╄→гoц情女王★ 提交于 2019-11-27 03:51:45
问题 It's been rehashed over and over that primitive types don't have constructors. For example this _bar is not initialized to 0 when I call Foo() : class Foo{ int _bar; }; So obviously int() is not a constructor. But what is it's name? In this example I would say i is: (constructed? initialized? fooed?) for(int i{}; i < 13; ++i) Loki Astari mentions here that the technique has some sort of name. EDIT in response to Mike Seymour: #include <iostream> using namespace std; class Foo{ int _bar;

How to serialize Java primitives using Jersey REST

吃可爱长大的小学妹 提交于 2019-11-27 02:10:26
In my application I use Jersey REST to serialize complex objects. This works quite fine. But there are a few method which simply return an int or boolean. Jersey can't handle primitive types (to my knowledge), probably because they're no annotated and Jersey has no default annotation for them. I worked around that by creating complex types like a RestBoolean or RestInteger, which simply hold an int or boolean value and have the appropriate annotations. Isn't there an easier way than writing these container objects? Have a look at Genson .It helped me a lot with a similar problem.With Genson

Is an int a 64-bit integer in 64-bit C#?

本小妞迷上赌 提交于 2019-11-27 01:57:21
In my C# source code I may have declared integers as: int i = 5; or Int32 i = 5; In the currently prevalent 32-bit world they are equivalent. However, as we move into a 64-bit world, am I correct in saying that the following will become the same? int i = 5; Int64 i = 5; No. The C# specification rigidly defines that int is an alias for System.Int32 with exactly 32 bits. Changing this would be a major breaking change. The int keyword in C# is defined as an alias for the System.Int32 type and this is (judging by the name) meant to be a 32-bit integer. To the specification: CLI specification

How does double to int cast work in Java

£可爱£侵袭症+ 提交于 2019-11-27 01:49:01
I am new to Java, and wondering how does double to int cast work ? I understand that it's simple for long to int by taking the low 32 bits, but what about double (64 bits) to int (32 bits) ? those 64 bits from double in binary is in Double-precision floating-point format (Mantissa), so how does it convert to int internally ? Jon Skeet It's all documented in section 5.1.3 of the JLS. In the first step, the floating-point number is converted either to a long, if T is long, or to an int, if T is byte, short, char, or int, as follows: If the floating-point number is NaN (§4.2.3), the result of the

Objective C Boolean Array

好久不见. 提交于 2019-11-27 01:28:08
问题 I need to utilize an array of booleans in objective-c. I've got it mostly set up, but the compiler throws a warning at the following statement: [updated_users replaceObjectAtIndex:index withObject:YES]; This is, I'm sure, because YES is simply not an object; it's a primitive. Regardless, I need to do this, and would greatly appreciate advice on how to accomplish it. Thanks. 回答1: Yep, that's exactly what it is: the NS* containers can only store objective-C objects, not primitive types. You

Check type of primitive field

﹥>﹥吖頭↗ 提交于 2019-11-27 01:01:28
问题 I'm trying to determine the type of a field on an object. I don't know the type of the object when it is passed to me but I need to find fields which are long s. It is easy enough to distinguish the boxed Long s but the primitive long seems more difficult. I can make sure that the objects passed to me only have Longs , not the primitives, but I'd rather not. So what I have is: for (Field f : o.getClass().getDeclaredFields()) { Class<?> clazz = f.getType(); if (clazz.equals(Long.class)) { //

Integer.class vs int.class

时光怂恿深爱的人放手 提交于 2019-11-27 00:53:10
问题 What is the difference between Integer.class , Integer.TYPE and int.class ? acc to me Integer.class is a reference of Integer (Wrapper) Class object but what is then int.class as int is not a class, it's a primitive type. And what does Integer.TYPE refer to? 回答1: From java.lang.Class.isPrimitive API There are nine predefined Class objects to represent the eight primitive types and void. These are created by the Java Virtual Machine, and have the same names as the primitive types that they