我想知道为什么有些东西必须在一对Apostrophes中,而其他内容必须在引号内?
void trythis(){
char myChar = 'Stuff';
String myString = "Blah";
int myInteger = '22';
Serial.print(myChar );
Serial.print(myString );
Serial.print(myInteger );
}
答案 0 :(得分:1)
字符文字使用单引号。因此,当您处理char
时,'x'
。
String 文字使用双引号。因此,当您处理string
时,"x"
。
char
是单个UTF-16代码单元 - 在大多数情况下"单个字符"。字符串是UTF-16代码单元的序列,即"一段文本" (几乎)任意长度。
你的最后一个例子,在编译之后,看起来像是:
int myInteger = 'x';
使用字符文字但后来隐式将其转换为int
- 相当于:
char tmp = 'x';
int myInteger = tmp;
答案 1 :(得分:1)
char ch = 'a';
string name = "Bob";
MainActivity:
public class MainActivity extends AppCompatActivity {
String[] IMGS={};
ArrayList<String> sal = new ArrayList<String>();
ListView listView;
public static final String KEY_USERID = "user_id";
private static final String REGISTER_URL = "http://example.com/Services.php?action=fetchUserLog";
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
registerUser();
listView = (ListView)findViewById(R.id.list);
}
///////////////////////////////////////// Volley Method Starts //////////////////////////////////////////////////////////////////
private void registerUser(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Toast.makeText(MainActivity.this,response,Toast.LENGTH_LONG).show();
try {
JSONObject jObj = new JSONObject(response);
String status = jObj.getString("status");
Toast.makeText(MainActivity.this, status, Toast.LENGTH_LONG).show();
// Now check status value
if (status.equals("0")) {
Toast.makeText(MainActivity.this, "There was some error! Please try again.", Toast.LENGTH_LONG).show();
}else if(status.equals("1")){
// Toast.makeText(getActivity(), "Information saved successfully", Toast.LENGTH_LONG).show();
JSONArray result = jObj.getJSONArray("results");
for(int i = 0 ; i < result.length() ; i++){
JSONObject json_data = result.getJSONObject(i);
sal.add(json_data.getString("images"));
//Toast.makeText(MainActivity.this, sal+"", Toast.LENGTH_LONG).show();
IMGS = sal.toArray(new String[sal.size()]);
listView.setAdapter(new ImageListAdapter(MainActivity.this, IMGS));
}
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(MainActivity.this, errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(MainActivity.this, "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERID, "21");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
///////////////////////////////////////// Volley Method Ends ////////////////////////////////////////////////////////////////////
}
答案 2 :(得分:0)
你编写的代码根本没有编译。
单引号用于字符文字(单个字符,在.NET中存储为UTF-16)。整数没有引用。
这是有效的:
char myChar = 's';
string myString = "Blah";
int myInteger = 22;