Questions tagged «java»

Java是一种流行的高级编程语言。如果您在使用或理解语言本身时遇到问题,请使用此标签。这个标签很少单独使用,最常与[spring],[spring-boot],[jakarta-ee],[android],[javafx],[gradle]和[maven]结合使用。

13
java:如何将变量从一种类型动态转换为另一种类型?
我想对Java变量进行动态转换,转换类型存储在其他变量中。 这是常规转换: String a = (String) 5; 这就是我要的: String theType = 'String'; String a = (theType) 5; 这有可能吗?谢谢! 更新资料 我正在尝试使用HashMap收到的A填充类。 这是构造函数: public ConnectParams(HashMap<String,Object> obj) { for (Map.Entry<String, Object> entry : obj.entrySet()) { try { Field f = this.getClass().getField(entry.getKey()); f.set(this, entry.getValue()); /* <= CASTING PROBLEM */ } catch (NoSuchFieldException ex) { …


6
如何在Java中将类型作为方法参数传递
在Java中,如何将类型作为参数传递(或声明为变量)? 我不想传递类型的实例,但要传递类型本身(例如,int,String等)。 在C#中,我可以这样做: private void foo(Type t) { if (t == typeof(String)) { ... } else if (t == typeof(int)) { ... } } private void bar() { foo(typeof(String)); } Java中有没有一种方法可以不传递类型t的实例? 还是我必须使用自己的int常量或枚举? 或者,还有更好的方法? 编辑:这是对foo的要求: 基于类型t,它生成另一个短的xml字符串。 if / else中的代码将非常小(一两行),并将使用一些私有类变量。
85 java  types 

4
正在运行的jmap无法打开套接字文件
我必须运行jmap才能进行进程的堆转储。但jvm返回: Unable to open socket file: target process not responding or HotSpot VM not loaded The -F option can be used when the target process is not responding 所以我用了-F: ./jmap -F -dump:format=b,file=heap.bin 10330 Attaching to process ID 10331, please wait... Debugger attached successfully. Server compiler detected. JVM version is 24.51-b03 …
85 java  linux  jvm  jvm-hotspot 

2
VisualVM-线程状态
有人可以解释我的区别Sleeping,Wait,Park,和Monitor在VisualVM的线程状态。 这是我发现的: Running:线程仍在运行。 Sleeping:线程正在睡眠(在线程对象上调用了method yield()方法) Wait:线程被互斥锁或屏障阻止,并且正在等待另一个线程释放锁 Park:停放的线程被挂起,直到获得许可。通常通过在线程对象上调用方法unpark()来取消线程 Monitor:线程正在等待条件变为真以恢复执行 我无法理解的是国家公园,实际上是什么暂停线程?如何在代码中检测导致线程挂起执行的原因? 有人可以在这方面指导我。 谢谢。
85 java  jvm  visualvm  jvisualvm 

7
在Java 8中有什么方法可以将ZoneId转换为ZoneOffset吗?
我通过method1有一个纪元和一个zoneId。它可以使用系统默认的zoneId转换为LocalDateTime,但是我找不到通过method2将纪元转换为LocalDateTime的方法,因为没有ZoneOffset.systemDefault。我认为它是晦涩的。 import java.time.{Instant, LocalDateTime, ZoneId, ZoneOffset} val epochSecond = System.currentTimeMillis() / 1000 LocalDateTime.ofInstant(Instant.ofEpochSecond(epochSecond), ZoneId.systemDefault())//method1 LocalDateTime.ofEpochSecond(epochSecond, 0, ZoneOffset.MAX)//method2

7
@Test之后的回滚事务
首先,我在StackOverflow上找到了很多与此相关的线程,但是它们都没有真正帮助我,因此很抱歉提出可能重复的问题。 我正在使用spring-test运行JUnit测试,我的代码如下所示 @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {}) public class StudentSystemTest { @Autowired private StudentSystem studentSystem; @Before public void initTest() { // set up the database, create basic structure for testing } @Test public void test1() { } ... } 我的问题是我希望我的测试不影响其他测试。所以我想为每个测试创建类似回滚的东西。我为此进行了很多搜索,但到目前为止我什么都没找到。我为此使用Hibernate和MySql

7
如何禁用特定URL的Spring Security
我正在使用无状态弹簧安全性,但如果要注册,我想禁用弹簧安全性。我禁用了 antMatchers("/api/v1/signup").permitAll(). 但它不起作用,我在下面收到错误消息: message=An Authentication object was not found in the SecurityContext, type=org.springframework.security.authentication.AuthenticationCredentialsNotFoundException 我认为这意味着弹簧安全过滤器正在工作 我的网址顺序始终为“ / api / v1” 我的春季配置是 @Override protected void configure(HttpSecurity http) throws Exception { http. csrf().disable(). sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS). and(). authorizeRequests(). antMatchers("/api/v1/signup").permitAll(). anyRequest().authenticated(). and(). anonymous().disable(); http.addFilterBefore(new AuthenticationFilter(authenticationManager()), BasicAuthenticationFilter.class); } 我的身份验证过滤器是 @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain …

7
是否有一个保留订单的Set也实现了List?
我试图找到的实现java.util.List,并java.util.Set在Java中的同一时间。我希望此类仅允许唯一元素(如Set)并保留其顺序(如List)。JDK 6中是否存在? 有List<T>#add(int, T)一点很重要,这样我就可以插入特定位置。
84 java  collections 

17
更改Google Maps API的“我的位置”按钮的位置
我正在使用Google Maps Android API v2,我需要一种方法来确定“我的位置”按钮的位置。 我得到这样的“我的位置”按钮: GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); final GoogleMap map = ((SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map)).getMap(); // This gets the button map.setMyLocationEnabled(true);

15
用.equals()和==运算符比较两个对象
我建立了一个String领域的课程。然后,我创建了两个对象,我也必须使用==运算符对其进行比较.equals()。这是我所做的: public class MyClass { String a; public MyClass(String ab) { a = ab; } public boolean equals(Object object2) { if(a == object2) { return true; } else return false; } public boolean equals2(Object object2) { if(a.equals(object2)) { return true; } else return false; } public static void main(String[] args) …
84 java  class  object  methods  equals 


11
在jUnit 4.x中执行Suite执行前后挂钩
我正在尝试使用jUnit 4.4执行一组集成测试的设置和拆卸。拆卸需要可靠地运行。我在使用TestNG时遇到其他问题,因此我希望移植回jUnit。在运行任何测试之前以及在完成所有测试之后,可以使用哪些挂钩执行? 注意:我们正在使用Maven 2进行构建。我已经尝试过使用maven的pre-&post-integration-test阶段,但是,如果测试失败,则maven会停止并且不会运行post-integration-test,这没有帮助。



By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.