literals

Will the Java compiler precalculate sums of literals?

你。 提交于 2019-12-23 16:18:26
问题 int i = 10 + 20; Is it true that the compiler will process this code, adding 10 + 20 , and the byte code is the same as for this line of code? int i = 30; Where can I read about it? 回答1: Yes, and you can even verify it for yourself. Take a small Java file, for example: public class Main { public Main() { int i = 10 + 20; } } Compile it with javac Main.java , and then run javap -c Main to disassemble it: Compiled from "Main.java" public class Main extends java.lang.Object{ public Main(); Code:

In C++, when I pass a value into a function, is it always converted to appropiate type?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:57:47
问题 If I have a function like void func(size_t x) and I call the function func(5) , is 5 immediately converted to size_t type? Does this hold generally for all types? I ask because I swear I've seen people write code where they do stuff like func(5.0) (passing 5 as a double) or func(0UL) (passing 0 as an unsigned long int). Is this really necessary? Can't we just pass in whatever we want, and C++ will treat it as the type that I used to define the function? 回答1: If there is an implicit conversion

Scala: How to escape a backtick in a literal?

点点圈 提交于 2019-12-23 09:05:06
问题 Literals in Scala allow to define identifier as this answer describes. Is there a way though to escape a backtick ` within a literal? To do something like: val `hello `world` = "hello world" Update : One of the use case for this is to use the refined library for some refined types that matches a regex containing a backtick, for instance: import eu.timepit.refined._ import eu.timepit.refined.api.Refined type MatchesRegexWithBacktick = String Refined MatchesRegex[W.`(a|`)`.T] 回答1: It can't be

ast.literal_eval() support for set literals in Python 2.7?

混江龙づ霸主 提交于 2019-12-23 07:37:05
问题 In the What’s New in Python 2.7 document it says that support for set literals was back-ported from Python 3.1. However it appears that this support was not extended to the ast module's literal_eval() function, as illustrated below. Was this intentional, an oversight, or something else -- and what are the cleanest workarounds for creating a literal set from a string representation? (I assume the following works in Python 3.1+, right?) import ast a_set = {1,2,3,4,5} print a_set print ast

Best practice for long string literals in Go

别来无恙 提交于 2019-12-23 06:48:23
问题 I've got a long string literal in Go: db.Exec("UPDATE mytable SET (I, Have, Lots, Of, Fields) = ('suchalongvalue', 'thisislongaswell', 'ohmansolong', 'wowsolong', 'loooooooooooooooooooooooooong')") I see two ways to make this more manageable: raw quotes, or multiple concatenated quotes: db.Exec(`UPDATE mytable SET (I, Have, Lots, Of, Fields) = ('suchalongvalue', 'thisislongaswell', 'ohmansolong', 'wowsolong', 'loooooooooooooooooooooooooong')`) db.Exec("UPDATE mytable SET (I, Have, Lots, Of,

Where the C++ literal-constant storage in memory?

旧时模样 提交于 2019-12-23 03:47:18
问题 Where the C++ literal-constant storage in memory? stack or heap? int *p = &2 is wrong. I want know why? Thanks ------------------------------------------------- My question is "Where the C++ literal-constant storage in memory", " int *p = &2 is wrong",not my question. 回答1: The details depend on the machine, but assuming a commonest sort of machine and operating system... every executable file contains several "segments" - CODE, BSS, DATA and some others. CODE holds all the executable opcodes.

what is the difference between 'Float a = 3f' and 'Float a = 3.0' in java?

这一生的挚爱 提交于 2019-12-22 08:11:07
问题 If I perform 97346822*3f, result is 2.9204048E8, however 97346822*3.0 gives me 2.92040466E8. Please explain. 回答1: The number 3.0 is the literal representation of a double value (it's equivalent to 3.0d ), whereas 3.0f is a float value. The different precisions explain why you're getting different results - a double is stored using 64-bits, a float uses 32-bits. 回答2: 97346822*3.0 will be treated as double . Based on oracle tutorial The double data type is a double-precision 64-bit IEEE 754

Are long-suffix and unsigned-suffix needed when declaring long literals in C++?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-21 20:59:05
问题 I have some ancient memories of writing C code like: long value = 0; in the bad old Win16 days and ending up with value being only half-initialized: i.e. the lower 16 bits were 0 and the upper 16 bits were whatever random bits were at that location in memory. As such, I became conditioned to write: long value = 0L; Is this still required in this day-and-age under C99 and/or C++? I know that ILP32 specifies that int and long are both 32-bit, but suppose we're using, say, LP64 where int s are

Java 9: What are collection factory methods? [closed]

最后都变了- 提交于 2019-12-21 06:41:16
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . The arrival of Java 9 brings many new features to Java's Collections API, one of which being collection factory methods. What are they and how can I implement them properly? 回答1: Note 1: To prevent the use of raw-types, I have opted to provide a generic type for each class that I

Is there any C++ style guide that talks about numeric literal suffixes?

隐身守侯 提交于 2019-12-21 04:26:17
问题 In all of the C++ style guides I have read, I never have seen any information about numerical literal suffixes (i.e. 3.14f , 0L , etc.). Questions Is there any style guide out there that talks about there usage, or is there a general convention? I occasionally encounter the f suffix in graphics programming. Is there any trend on there usage in the type of programming domain? 回答1: There is no general style guide that I've found. I use capital letters and I'm picky about using F for float