当我要执行以下代码时,我认为签名的int我将被转换为unsigned,结果将是一些大的正数,但结果是2
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.jfx;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
/**
*
* @author srahman
*/
public class FXMLDocumentController implements Initializable {
private Label label;
@FXML
private TableView<CountryModel> tblCountryPopulation;
@FXML
private TableColumn<CountryModel, String> tcCountryName;
@FXML
private TableColumn<CountryModel, Integer> tcPopulation;
@FXML
private TableView<CityModel> tblCityPopulation;
@FXML
private TableColumn<CityModel, String> tcCityName;
@FXML
private TableColumn<CityModel, Integer> tcCityPopulation;
private boolean mCountryThreadStatus = true;
private List<CountryModel> mCountryData = null;
private ObservableList<CountryModel> mCountryObservableList = null;
private List<CountryModel> mTempCountryData = null;
private Thread mCountryTableThread = null;
private boolean mCityThreadStatus = true;
private List<CityModel> mCityData = null;
private ObservableList<CityModel> mCityObservableList = null;
private List<CityModel> mTempCityData = null;
private Thread mCityTableThread = null;
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
tcCountryName.setCellValueFactory(new PropertyValueFactory<CountryModel, String>("CountryName"));
tcCountryName.setSortable(false);
tcPopulation.setCellValueFactory(new PropertyValueFactory<CountryModel, Integer>("Population"));
tcPopulation.setSortable(false);
tcCityName.setCellValueFactory(new PropertyValueFactory<CityModel, String>("CityName"));
tcCityName.setSortable(false);
tcCityPopulation.setCellValueFactory(new PropertyValueFactory<CityModel, Integer>("Population"));
tcCityPopulation.setSortable(false);
UpdateCountryPopulation();
UpdateCityPopulation();
}
public void UpdateCountryPopulation() {
Platform.runLater(new Runnable() {
@Override
public void run() {
// run in a second
final long timeInterval = 100;
Runnable runnable = () -> {
while (mCountryThreadStatus) {
// ------- code for task to run
mCountryData = GetCountryData();
if (mCountryData != null && tblCountryPopulation != null) {
try {
synchronized (mCountryData) {
mCountryObservableList = FXCollections.observableList(mCountryData);
if (mCountryObservableList.size() > 0 && mCountryObservableList != null) {
tblCountryPopulation.setItems(mCountryObservableList);
tblCountryPopulation.refresh();
}
}
} catch (Exception aEx) {
System.out.println("Exception happend while inserting item in view: " + aEx.getMessage());
}
}
if (mCountryData != null && mCountryObservableList != null) {
mCountryObservableList = null;
mCountryData = null;
}
// ------- ends here
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
if (mCountryThreadStatus) {
mCountryTableThread = new Thread(runnable);
mCountryTableThread.start();
System.out.println("Current thread ID: " + Thread.currentThread().getId());
}
}
});
}
public List<CountryModel> GetCountryData() {
Random lRandom = new Random();
int lTemp = lRandom.nextInt(15 - 5 + 1) + 5;
int lPopulation = lRandom.nextInt(9999) + 1;
try {
if (mTempCountryData == null) {
mTempCountryData = new ArrayList<>();
mTempCountryData.add(new CountryModel("Test" + lTemp, lPopulation));
} else {
CountryModel lCountryModel = FindInExistinData("Test" + lTemp);
if (lCountryModel == null) {
mTempCountryData.add(new CountryModel("Test" + lTemp, lPopulation));
} else {
lCountryModel.setCountryName("Test" + lTemp);
lCountryModel.setPopulation(lPopulation);
}
}
} catch (Exception aEx) {
System.out.println("Exception : " + aEx.getMessage());
}
return mTempCountryData;
}
private CountryModel FindInExistinData(String aCountry) {
for (int j = 0; j < mTempCountryData.size(); j++) {
if (mTempCountryData.get(j).getCountryName().equals(aCountry)) {
return mTempCountryData.get(j);
}
}
return null;
}
public void UpdateCityPopulation() {
Platform.runLater(new Runnable() {
@Override
public void run() {
// run in a second
final long timeInterval = 100;
Runnable runnable = () -> {
while (mCityThreadStatus) {
// ------- code for task to run
mCityData = GetCityData();
if (mCityData != null && tblCityPopulation != null) {
try {
synchronized (mCityData) {
mCityObservableList = FXCollections.observableList(mCityData);
if (mCityObservableList.size() > 0 && mCityObservableList != null) {
tblCityPopulation.setItems(mCityObservableList);
tblCityPopulation.refresh();
}
}
} catch (Exception aEx) {
System.out.println("Exception happend while inserting item in view: " + aEx.getMessage());
}
}
if (mCityData != null && mCityObservableList != null) {
mCityObservableList = null;
mCityData = null;
}
// ------- ends here
try {
Thread.sleep(timeInterval);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
if (mCityThreadStatus) {
mCityTableThread = new Thread(runnable);
mCityTableThread.start();
System.out.println("Current thread ID: " + Thread.currentThread().getId());
}
}
});
}
public List<CityModel> GetCityData() {
Random lRandom = new Random();
int lTemp = lRandom.nextInt(15 - 5 + 1) + 5;
int lPopulation = lRandom.nextInt(9999) + 1;
try {
if (mTempCityData == null) {
mTempCityData = new ArrayList<>();
mTempCityData.add(new CityModel("Test" + lTemp, lPopulation));
} else {
CityModel lCityModel = FindInExistinCityData("Test" + lTemp);
if (lCityModel == null) {
mTempCityData.add(new CityModel("Test" + lTemp, lPopulation));
} else {
lCityModel.setCityName("Test" + lTemp);
lCityModel.setPopulation(lPopulation);
}
}
} catch (Exception aEx) {
System.out.println("Exception : " + aEx.getMessage());
}
return mTempCityData;
}
private CityModel FindInExistinCityData(String aCity) {
for (int j = 0; j < mTempCityData.size(); j++) {
if (mTempCityData.get(j).getCityName().equals(aCity)) {
return mTempCityData.get(j);
}
}
return null;
}
void CleanUpThread() {
mCountryThreadStatus = false;
mCityThreadStatus = false;
}
}
答案 0 :(得分:5)
执行加法时,会对操作数执行常规算术转换,其状态为([expr]/11.5.3):
- 否则,如果具有无符号整数类型的操作数的等级大于或等于另一个操作数的类型的等级, 带有符号整数类型的操作数应转换为类型 带有无符号整数类型的操作数。
所以-8
转换为无符号数。根据{{3}}的内容如下:
如果目标类型是无符号的,则结果值最小 无符号整数与源整数一致(模2 n 其中n是用于表示无符号类型的位数。)
将我们的号码翻译为(MAX_UINT + 1) - 8
。现在,因为无符号加法是模块化的2 n ([conv.integral]/2):
无符号整数应遵守算术模2 n 的定律,其中n是该特定整数大小的值表示中的位数。
结果必须为(MAX_UINT + 1) - 8 + 10
模MAX_UINT + 1
,而且只是2
。